-2

The following code gave me Datetimestamp as [ 2020-07-183 17:07:55.551 ]. The issue is with "Day" in Datetimestamp, which has three digits. How to format currentTimeMillis into the right format for day of month?

 public String Datetimesetter(long currentTimeMillis, SimpleDateFormat dateFormat) {
        
        dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
        // Create a calendar object that will convert the date and time value in milliseconds to date.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(currentTimeMillis);
        return dateFormat.format(calendar.getTime());
    }

SOLUTION WHICH WORKED FOR ME:

Please visit this link.

MdBasha
  • 423
  • 4
  • 16
  • in which format your want the output? – Uzair S. Jul 01 '20 at 14:17
  • 2
    Avoid the use of `java.util.Date` if you can... What's the minimum API level your app is supporting? The 3 digits of the day are the *day of year*, most likely... – deHaar Jul 01 '20 at 14:18
  • 2
    Basically, you need to read the documentation of `SimpleDateFormat` very closely. Your pattern is wrong - you probably meant `yyyy-MM-dd HH:mm:ss.SSS` – Jon Skeet Jul 01 '20 at 14:20
  • 2
    But I'd agree with deHaar that it's best to avoid java.util.Date where possible. – Jon Skeet Jul 01 '20 at 14:20
  • 1
    various solutions here --> https://stackoverflow.com/questions/18929929/convert-timestamp-into-current-date-in-android – Wini Jul 01 '20 at 14:22

2 Answers2

3

This is for the case you are supporting Apps from API level 26 (native support of java.time) or you are willing / allowed to use a backport library of the same functionality.

Then you can use a correct / matching pattern (one that considers three-digit days) like this:

public static void main(String[] args) {
    // mock / receive the datetime string
    String timestamp = "2020-07-183 17:07:55.551";
    // create a formatter using a suitable pattern (NOTE the 3 Ds)
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-DDD HH:mm:ss.SSS");
    // parse the String to a LocalDateTime using the formatter defined before
    LocalDateTime ldt = LocalDateTime.parse(timestamp, dtf);
    // and print its default String representation
    System.out.println(ldt);
}

which outputs

2020-07-01T17:07:55.551

So I guess the day of year no. 183 was actually July 1st.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    FYI, it seems the back-port may no longer be necessary to support a [subset of *java.time*](https://developer.android.com/studio/write/java8-support-table) on earlier Android because of the new feature: [API desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring). – Basil Bourque Jul 01 '20 at 16:05
  • 1
    deHaar, I am pretty convinced that the OP wanted to *format* a count of milliseconds since the epoch into a pretty conventional date-time string. At least I took the string in the question to be *the observed result* of formatting (not the *input* into the program). I have edited the question text to agree with the code in the question, I hope it’s clearer (and in agreement with the OP’s intentions). As you know, I applaud the use of java.time. – Ole V.V. Jul 01 '20 at 20:36
  • @OleV.V. Thanks for the comment, sorry for the delay, I read the comment on my mobile device, wanted to answer later on but simply forgot it. Having re-read the question I now think you are right, my answer was not fully covering all aspects of the question. It seems the answer was suitable for acceptance, but as you said, it doesn't fully address the requirements. – deHaar Jul 03 '20 at 06:23
  • 1
    Yes, I saw the answer was accepted. So maybe I was wrong. @MdBasha, if only for our curiosity, would you clarify? – Ole V.V. Jul 03 '20 at 06:26
  • Sure, I will post the implementation steps of my side, @OleV.V. – MdBasha Jul 07 '20 at 11:30
  • Here is the link [timestampdecode](https://stackoverflow.com/a/62774455/13031115), @OleV.V. where I posted an answer as this question is closed and not open for answer. Thank you all! – MdBasha Jul 07 '20 at 11:49
  • Datetimestamp question above has three digits (183) for "Day" which is simply [ July 1, 2020 (Wed) ] as per this info [table](https://www.epochconverter.com/days/2020). Check for "Day 183". Hope it helps!. – MdBasha Jul 07 '20 at 12:03
3

your date format is incorrect

dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");

change to this

dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS.SSS");
Sufwan Ansari
  • 131
  • 1
  • 6
  • 2
    This class is part of terrible date-time classes bundled with the earliest versions of Java, supplanted years ago by the *java.time* classes. For modern solution, see [Answer by deHaar](https://stackoverflow.com/a/62679280/642706). – Basil Bourque Jul 01 '20 at 16:01