Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
Here is my timestamp string
2000-01-01T12:00:00.000Z

I need to convert it to Readable date and time format i.e "Saturday January 1st, 2000 12:00 am"

like image 569
Rohit Avatar asked Jul 16 '26 22:07

Rohit


2 Answers

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;
}
like image 92
real 19 Avatar answered Jul 18 '26 12:07

real 19


I had the same issue and I tried @real19 answer but it gave me errors. 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

like image 32
Jackyto Avatar answered Jul 18 '26 12:07

Jackyto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!