在Android中,如何将以下时间戳格式化为可读字符串?

在Android中,如何将以下时间戳格式化为可读字符串?,android,timestamp,Android,Timestamp,我的应用程序从具有以下时间戳结构的网站中提取文章: 2017-04-25T00:00:00-05:00 如何将其转换为以下格式: April 25, 2017 12:00 AM 谢谢。您可以使用时间戳格式化字符串,如: SimpleDateFormat smDateFormat = new SimpleDateFormat("MMMM d, yyyy hh:mm"); String dateString = smDateFormat.format([timestamp]); 编辑: 我试着自

我的应用程序从具有以下时间戳结构的网站中提取文章:

2017-04-25T00:00:00-05:00

如何将其转换为以下格式:

April 25, 2017 12:00 AM

谢谢。

您可以使用时间戳格式化字符串,如:

SimpleDateFormat smDateFormat = new SimpleDateFormat("MMMM d, yyyy hh:mm");
String dateString = smDateFormat.format([timestamp]);
编辑:

我试着自己做这个

Timestamp timestamp = new Timestamp(System.currentTimeMillis());

SimpleDateFormat smDateFormat = new SimpleDateFormat("MMMM dd, yyyy - HH:mm", Locale.US);
String dateString = smDateFormat.format(timestamp);

Toast.makeText(this, dateString, Toast.LENGTH_LONG).show();
…输出为:


您可以使用时间戳格式化字符串,如:

SimpleDateFormat smDateFormat = new SimpleDateFormat("MMMM d, yyyy hh:mm");
String dateString = smDateFormat.format([timestamp]);
编辑:

我试着自己做这个

Timestamp timestamp = new Timestamp(System.currentTimeMillis());

SimpleDateFormat smDateFormat = new SimpleDateFormat("MMMM dd, yyyy - HH:mm", Locale.US);
String dateString = smDateFormat.format(timestamp);

Toast.makeText(this, dateString, Toast.LENGTH_LONG).show();
…输出为:


我相信有一种更简单的方法可以做到这一点,但以下是我创建的解决方案:

public static String convertADevotionTimestamp(String time){
        String year = time.substring(0,4);
        String month = time.substring(5,7);
        String date = time.substring(8,10);
        String hour = time.substring(11,13);
        String minute = time.substring(14,16);

        // Format month
        int monthNum = Integer.parseInt(month);
        String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        month = months[monthNum - 1];

        // Format AM vs PM
        String amOrPm = null;
        int hourNum = Integer.parseInt(hour);
        if (hourNum >= 12){
            amOrPm = "pm";
            hour = String.valueOf(hourNum - 12);
        } else {
            amOrPm = "am";
            if (hourNum == 0){
                hour = "12";
            }
        }

        return month + " " + date + ", " + year + " at " + hour + ":" + minute + amOrPm + " ET";
    }

我相信有一种更简单的方法可以做到这一点,但以下是我创建的解决方案:

public static String convertADevotionTimestamp(String time){
        String year = time.substring(0,4);
        String month = time.substring(5,7);
        String date = time.substring(8,10);
        String hour = time.substring(11,13);
        String minute = time.substring(14,16);

        // Format month
        int monthNum = Integer.parseInt(month);
        String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        month = months[monthNum - 1];

        // Format AM vs PM
        String amOrPm = null;
        int hourNum = Integer.parseInt(hour);
        if (hourNum >= 12){
            amOrPm = "pm";
            hour = String.valueOf(hourNum - 12);
        } else {
            amOrPm = "am";
            if (hourNum == 0){
                hour = "12";
            }
        }

        return month + " " + date + ", " + year + " at " + hour + ":" + minute + amOrPm + " ET";
    }

看看,它能帮你做你想做的事。看看,它能帮你做你想做的事。那没用。得到以下错误:java.lang.IllegalArgumentException:无法在java.text.DateFormat.format(DateFormat.java:302)将给定对象格式化为日期。如果您仍然需要它,请检查编辑,它可能是“hh”或“d”而不是“hh”和“dd”,但我并不认为这样做不起作用。得到以下错误:java.lang.IllegalArgumentException:无法在java.text.DateFormat.format(DateFormat.java:302)将给定对象格式化为日期。如果您仍然需要它,请检查编辑,它可能是“hh”或“d”而不是“hh”和“dd”,但我并不这么认为