1

if i try the below code for eg:

i will set the date as Fri July 25 2014 10:00 AM. which gives date in milliseconds as 1402080056000,

now if i try to read the same milliseconds to date as below

  long time = 1402080056000;

  Date mydate = new Date(time); 

mydate variable shows date as Sat Jun 25 00:10:56 IST 2014

  String DateTimeString = DateFormat.getDateTimeInstance().format(new Date(time));

with the above statement in DateTimeString i get date as Jun 25 , 2014 12:10:56 AM

How to read the datetime present in 1402080056000 to Fri July 25 2014 10:00 AM

Naruto
  • 9,476
  • 37
  • 118
  • 201

4 Answers4

3

Just need to work on the format string,

String dateTimeString= 
String.valueOf(new SimpleDateFormat("dd-MM-yyyy hh:mm").format(new Date(time)));  

Explicitly set time zone:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = String.valueOf(dateFormat.format(millis));  

Also, this would be useful Regarding Timezones and Java

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51
  • if i pass 0 as time, i will get date as 01-01-1970 05:30. Why not its giving GMT format time. i.e it should give me 01-01-1970 00:00 – Naruto Apr 18 '14 at 13:14
0

try below code:

private String convertMilliToDate(long timestamp) {

        DateFormat formatter = new SimpleDateFormat("YOUR DATE FORMAT");

        // Create a calendar object that will convert the date and time value in
        // milliseconds to date.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return formatter.format(calendar.getTime());
    }
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • Hi, thanks, if i pass timestamp as 0, i get datetime as 01:01:70:05:30:00. But why i'm getting 5.30 AM, it should be 00 right? – Naruto Apr 18 '14 at 12:56
  • 1
    if you will give wrong data to convert then by default it will convert to this date like 1-1-1970. Because timestamp calculations starts from that date. So you must need to give valid timestamp to convert. – Pratik Dasa Apr 21 '14 at 04:48
0

Try this:

String date= DateFormat.format("dd/MM/yyyy hh:mm:ss", new Date(date_in_milis)).toString();
Noman
  • 4,049
  • 10
  • 38
  • 59
  • Hi, i need date in GMT format, but why i get as 5.30 IST, – Naruto Apr 18 '14 at 12:58
  • @Naruto .. use this.. you will get in GMT :) – Noman Apr 18 '14 at 12:58
  • Thank u, but i'm getting error, The method format(Date) in the type DateFormat is not applicable for the arguments (String, Date) :( – Naruto Apr 18 '14 at 13:00
  • @Naruto .. try now... i have edited... just put your miliseconds in place of date_in_milis... and tell me – Noman Apr 18 '14 at 13:02
  • i tried, still i'm getting error as The method format(Date) in the type DateFormat is not applicable for the arguments (String, Date) – Naruto Apr 18 '14 at 13:03
  • long DateinMilis = 0132456789; .....String installedDate = DateFormat.format("dd/MM/yyyy hh:mm:ss", new Date(DateinMilis )).toString(); ............. Its working for me bro... Just put the long date (miliseconds) and get date as String.. – Noman Apr 18 '14 at 13:05
  • nope dude, i tried still i'm getting the error in the commented string. here is what i tried String installedDate = DateFormat.format("dd/MM/yyyy hh:mm:ss", new Date(timestamp)).toString();. i get error as "The method format(Date) in the type DateFormat is not applicable for the arguments (String, Date)" – Naruto Apr 18 '14 at 13:10
  • ha ha nomi, true. anyway i found answer here http://stackoverflow.com/questions/6730797/convert-gmt-datetime-string, thanks for help nomi – Naruto Apr 18 '14 at 13:20
  • Nomi, one more question. Do you know Google tasks uses which date formate is it GMT? – Naruto Apr 18 '14 at 13:21
  • I am glad u r happy ;-) – Noman Apr 18 '14 at 13:21
  • Mmmm... never looked for it buddy.. no idea :) – Noman Apr 18 '14 at 13:30
  • Thanks for sharing.. keep it up :) – Noman Apr 18 '14 at 13:37
0

Date-time work is easier using the Joda-Time library, which works on Android.

long millis = 1402080056000L;

DateTimeZone timeZoneIndia = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( millis, timeZoneIndia );

DateTime dateTimeFrance = dateTimeIndia.withZone( DateTimeZone.forID( "Europe/Paris" ) );

DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );

To parse a string as a date-time, search StackOverflow for "Joda parse" to find many examples.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154