首页 > 解决方案 > 如何获取特定字符串格式的日期完整时间

问题描述

我有一个 API 包含格式为“/Date(1527677209864)/”的字符串日期,如何获取要在 Android 应用程序中使用的日期和时间

标签: androiddatetime

解决方案


我假设 1527677209864 值是时间戳,对吗?试试这个功能:

public static String getDateAndTime(@NotNull Context context, long timestamp) {

        Date date = new Date(timestamp * 1000);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        DateFormat df = new SimpleDateFormat(getTimeFormat(context), Locale.getDefault());

        return df.format(date);

    }

让我知道这是否是您想要的。


推荐阅读