Convert Epoch time and date to Epoch time in Android

StrDate = "2011-07-19T18: 23: 20 + 0000";

How can I get an era time for a specified date format in android

I would also like to know how to convert an era time to a specified date format.

I would appreciate a direct answer with an example.

+5
source share
3 answers

Sample code using Joda-Time 2.3.

Unix time is the number of seconds since the beginning of 1970 in UTC / GMT format.

How can I get an era time for a specified date format in android

DateTime dateTimeInUtc = new DateTime( "2011-07-19T18:23:20+0000", DateTimeZone.UTC );
long secondsSinceUnixEpoch = ( dateTimeInUtc.getMillis() / 1000 ); // Convert milliseconds to seconds.

... and ...

how to convert an era time to a specified date format.

String dateTimeAsString = new DateTime( secondsSinceUnixEpoch * 1000, DateTimeZone.UTC ).toString();

To reset these values ​​to the console ...

System.out.println( "dateTimeInUtc: " + dateTimeInUtc );
System.out.println( "secondsSinceUnixEpoch: " + secondsSinceUnixEpoch );
System.out.println( "dateTimeAsString: " + dateTimeAsString );

Bonus: Set to a different time zone.

DateTime dateTimeMontréal = dateTimeInUtc.withZone( DateTimeZone.forID( "America/Montreal" ) );
+5

SimpleDateFormat. .

:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZZZZ");
Date gmt = formatter.parse("2011-07-19T18:23:20+0000");
long millisecondsSinceEpoch0 = gmt.getTime();
String asString = formatter.format(gmt);

, Date Java 0, UTC/GMT, , .

+6

To answer your question a little late, but Joda-Time will be able to process both in a simple and clean way.

Using Joda-Time

1.Epoch time

Where is the date of your time in an era

DateTime dateTime = new DateTime(date*1000L);
System.out.println("Datetime ..." + dateTime);

Datetime from Epoch ...2014-08-01T13:00:00.000-04:00

2. Date to the era

 DateTime fromDate = new DateTime("2011-07-19T18:23:20+0000");
 long epochTime =  fromDate.getMillis();
 System.out.println("Date is.." + fromDate + " epoch of date " + epochTime);

 Date is..2011-07-19T14:23:20.000-04:00 epoch of date 1311099800000
+1
source

All Articles