0

I what to convert selected date to milliseconds without timezone difference. Below is my code.

 String selectedDate=Jan 18, 2020;
 SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy", Locale.US);
 Date date = format.parse(selectedDate);

while running I am getting date like Sat Jan 18 00:00:00 GMT+05:30 2020. But I want without timezone difference like Jan 18, 2020.

thiru
  • 45
  • 1
  • 6
  • `LocalDate ld = LocalDate.parse(selectedDate, DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.US));` – Youcef LAIDANI Jan 18 '20 at 13:49
  • @YCF_L- this function is available above SDK 26. For the lower end what needs to do? – thiru Jan 18 '20 at 13:54
  • What about using https://www.threeten.org I think It can be better to use it – Youcef LAIDANI Jan 18 '20 at 13:56
  • On executing what are you getting this long format date? Is it some ```System.out.print()``` call? That is, you have a ```Date``` instance as you have shown in the code. After that, how are you getting this representation of the date? – Sree Kumar Jan 18 '20 at 14:38
  • Here my problem is GMT time. I need to convert Jan 18, 2020, to milliseconds without adding GMT time. – thiru Jan 18 '20 at 15:31
  • But there has to be some timezone, isn't it? Is your question only about getting a formatted string which doesn't have the timezone or is it that you want the ```Date``` object not to have timezone, which is not possible. It will be at least GMT +00:00, I mean. – Sree Kumar Jan 18 '20 at 16:47
  • @Sree - Yes, I am getting the Date object with the current timezone. But my requirement is to convert date(Jan 18, 2020) to milliseconds without timezone difference. I don't need time zone differences. Is there is any way possible? – thiru Jan 20 '20 at 06:40
  • What do you mean by *without timezone difference*? It’s never the same date in all time zones, so you need to settle on a time zone or offset from UTC for your question to make sense. Do you want the time at the start of the day in UTC, that is, at offset zero (0)? – Ole V.V. Jan 26 '20 at 07:17

1 Answers1

2

For a difference of 0 from UTC:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MMM dd, uuuu", Locale.ENGLISH);
    String selectedDate = "Jan 18, 2020";
    ZonedDateTime zdt = LocalDate.parse(selectedDate, dateFormatter)
            .atStartOfDay(ZoneOffset.UTC);
    System.out.println(zdt);
    long millisSinceEpoch = zdt.toInstant().toEpochMilli();
    System.out.println(millisSinceEpoch);

Output from this snpipet is:

2020-01-18T00:00Z
1579305600000

I am using java.time, the modern Java date and time API. The classes that you used, SimpleDateFormat and Date, are poorly designed and long outdated, and no one should use them anymore.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161