0

I am converting milliseconds to the time of the respective country time format, for example, pakistan, US etc

For example timeinmilliseconds=1549362600000 So its respective Time formate from which I got these milliseconds is 15:30 or 3:30 in 12 hr format When I want to convert these milliseconds back to that time I get 10:30 (Five hrs back)

public String getTimeFromLong(long timeInMilliseconds){
String mytime="";
long minute = (timeInMilliseconds / (1000 * 60)) % 60;
long hour = (timeInMilliseconds / (1000 * 60 * 60)) % 24;
mytime = String.format("%02d:%02d", hour, minute);
return mytime;
}

If I select time 4:00 I converted to that to milliseconds (This part is OK) And wants the time back from milliseconds but get five hours back For example, If I select time 9:30 convert it to milliseconds and then to time I get 4:30

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43

4 Answers4

2

You need to use your local time zone to get the time in your region, the default is being apllied which is the Greenwich Mean Time (GMT). For Pakistan use Asia/Karachi like so:

SimpleDateFormat simpleDateFormat= new SimpleDateFormat("hh:mm");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Karachi"));
Oush
  • 3,090
  • 23
  • 22
  • 1
    You are using terrible date-time classes that were supplanted years ago by the modern *java.time* classes with the adoption of JSR 310. – Basil Bourque Feb 05 '19 at 07:03
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Feb 05 '19 at 13:53
0

Use this method to convert milliseconds into your local time

public String getTime(long time){
    Calendar calendar = new GregorianCalendar();
    calendar.setTimeInMillis(time);

    SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
    Date date = new Date(time);
    String kTime = format.format(date);     
    return kTime;
}
Ashmeet Arora
  • 164
  • 1
  • 3
  • 11
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Feb 05 '19 at 13:53
0

Using Java 8 we can do the following.

LocalDateTime dateTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneId.systemDefault());

to get date and time

Mohammad Javed
  • 328
  • 3
  • 13
  • `LocalDateTime` is the wrong class to use here. Lacking any concept of time zone or offset-from-UTC, it cannot represent a moment. You are starting with a moment and then throwing away the valuable zone/offset info with nothing gained. – Basil Bourque Feb 05 '19 at 07:00
0

Use below code to get time from long values:

public String getTimeFromLong(long timeInMilliseconds){
 // Creating date format
 DateFormat simple = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z");
 Date result = new Date(timeInMilliseconds);
  return simple.format(result);
}
Rishav Singla
  • 485
  • 4
  • 10
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Feb 05 '19 at 13:53