Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

datetime - Android convert date and time to milliseconds

I have one date and time format as below:

Tue Apr 23 16:08:28 GMT+05:30 2013

I want to convert into milliseconds, but I actually dont know which format it is. Can anybody please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update for DateTimeFormatter introduced in API 26.

Code can be written as below for API 26 and above

// Below Imports are required for this code snippet
// import java.util.Locale;
// import java.time.LocalDateTime;
// import java.time.ZoneOffset;
// import java.time.format.DateTimeFormatter;
String date = "Tue Apr 23 16:08:28 GMT+05:30 2013";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
LocalDateTime localDate = LocalDateTime.parse(date, formatter);
long timeInMilliseconds = localDate.atOffset(ZoneOffset.UTC).toInstant().toEpochMilli();
Log.d(TAG, "Date in milli :: FOR API >= 26 >>> " + timeInMilliseconds);
// Output is -> Date in milli :: FOR API >= 26 >>> 1366733308000

But as of now only 6% of devices are running on 26 or above. So you will require backward compatibility for above classes. JakeWharton has been written ThreeTenABP which is based on ThreeTenBP, but specially developed to work on Android. Read more about How and Why ThreeTenABP should be used instead-of java.time, ThreeTen-Backport, or even Joda-Time

So using ThreeTenABP, above code can be written as (and verified on API 16 to API 29)

// Below Imports are required for this code snippet
// import java.util.Locale;
// import org.threeten.bp.OffsetDateTime;
// import org.threeten.bp.format.DateTimeFormatter;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
        "EEE MMM dd HH:mm:ss OOOO yyyy", Locale.ROOT);
String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013";
long timeInMilliseconds = OffsetDateTime.parse(givenDateString, formatter)
        .toInstant()
        .toEpochMilli();
System.out.println("Date in milli :: USING ThreeTenABP >>> " + timeInMilliseconds);
// Output is -> Date in milli :: USING ThreeTenABP >>> 1366713508000

Ole covered summarised information (for Java too) in his answer, you should look into.


Below is old approach (and previous version of this answer) which should not be used now

Use below method

String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013"; 
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
    Date mDate = sdf.parse(givenDateString);
    long timeInMilliseconds = mDate.getTime();
    System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
            e.printStackTrace();
}

Read more about date and time pattern strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...