简体   繁体   English

Android 将 Ruby on Rails 时间戳转换为日期时间

[英]Android Convert Ruby on Rails Timestamp to Date Time

I am building an Android App, Fetching Data from API that is build in Ruby On Rails Framework.我正在构建一个 Android 应用程序,从在 Ruby On Rails 框架中构建的 API 中获取数据。
Here is my timestamp string这是我的时间戳字符串
2000-01-01T12:00:00.000Z 2000-01-01T12:00:00.000Z

I need to convert it to Readable date and time format ie "Saturday January 1st, 2000 12:00 am"我需要将其转换为可读的日期和时间格式,即“2000 年 1 月 1 日星期六上午 12:00”

You can try this:你可以试试这个:

public static String convertDate(String oldDateString){
    String format = "yyyy-MM-dd'T'HH:mm:ss.sssZZZZ";

    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = null;
    try {
        date = sdf.parse(oldDateString);
    } catch (ParseException e) {
        // handle exception here !
    }

    String newFormatString = "MMMM dd, yyyy 'at' HH:mm a";

    SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);



    String newDateString = newFormatter.format(date);

    return newDateString;
}

I had the same issue and I tried @real19 answer but it gave me errors.我遇到了同样的问题,我尝试了 @real19 答案,但它给了我错误。 I came with that then :我带着那个来:

  public static String convertDate(String oldDateString){
    String format = "yyyy-MM-dd'T'HH:mm:ss.sss";

    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = null;
    try {
        date = sdf.parse(oldDateString);
    } catch (ParseException e) {
        // handle exception here !
        e.printStackTrace();
    }

    String newFormatString = "EEEE MMMM dd, yyyy 'at' HH:mm a";

    SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);

    String newDateString = newFormatter.format(date);

    return newDateString;
}

Formatting gave me jeudi décembre 22, 2016 at 16:42 PM but you can adjust your DateFormat格式给了我jeudi décembre 22, 2016 at 16:42 PM但你可以调整你的 DateFormat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM