简体   繁体   中英

Android convert gmt time to readable date

In android I would like to convert "2015-11-10T17:00:00-0500" to a readable date format such as :

Nov 31, 2015
Nov 31, 2015 4:00:00 PM
4:00:00 PM 12:00:00 AM

what would be the best way to do this, I have tried and failed using the Date method

Just use SimpleDateFormat, something like this:

String time = "2015-11-10T17:00:00";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm:ss dd.MM.yyyy");
try {
    Date date = dateFormat.parse(time);

    String out = dateFormat2.format(date);
    Log.e("Time", out);
} catch (ParseException e) {
}

java.time

The legacy date-time API ( java.util date-time types and their formatting type, SimpleDateFormat ) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time , the modern date-time API * .

Your date-time string has a timezone offset of -0500 ie the corresponding date-time in UTC is 2015-11-10T22:00:00Z where Z stands for Zulu and is the timezone designator for zero-timezone offset ie Etc/UTC timezone which has the timezone offset of +00:00 hours.

The type which represents date-time along with the timezone offset is OffsetDateTime . In order to format it into different patterns, you need a DateTimeFormatter .

Demo:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d'T'H:m:sXX", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse("2015-11-10T17:00:00-0500", dtfInput);

        DateTimeFormatter dtfOutput1 = DateTimeFormatter.ofPattern("EEE dd, uuuu", Locale.ENGLISH);
        DateTimeFormatter dtfOutput2 = DateTimeFormatter.ofPattern("EEE dd, uuuu h:mm:ss a", Locale.ENGLISH);
        DateTimeFormatter dtfOutput3 = DateTimeFormatter.ofPattern("h:mm:ss a", Locale.ENGLISH);

        String output1 = dtfOutput1.format(odt);
        String output2 = dtfOutput2.format(odt);
        String output3 = dtfOutput3.format(odt);

        System.out.println(output1);
        System.out.println(output2);
        System.out.println(output3);
    }
}

Output:

Tue 10, 2015
Tue 10, 2015 5:00:00 PM
5:00:00 PM

Learn more about the modern date-time API * from Trail: Date Time .


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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