2

I have a date in this format "2017-05-17 00:31:16.227", I need to convert it to "17-MAY-17 12.31.16.227000000 AM".

String convertTimestamp(Timestamp date) {

    String newstring = null;
    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSSSSS");
        java.util.Date date1 = dateFormat.parse(date.toString());
        DateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS aa");
        newstring = sdf.format(date1);

    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("new string" + newstring);

    return newstring;
}

but I am getting the value like this **"17-MAY-17 12.31.16.000000227 AM"

Ranjith Reddy
  • 183
  • 2
  • 12
  • 1
    Just use `yyyy-MM-dd hh:mm:ss.SSS000000`. – Dawood ibn Kareem May 18 '17 at 01:31
  • date can be like this 2017-05-17 00:31:16.227111(max 6 characters in nano seconds) @DawoodibnKareem – Ranjith Reddy May 18 '17 at 01:52
  • Good luck getting that into a `java.util.Date`. – Dawood ibn Kareem May 18 '17 at 01:55
  • Nine digits in fraction means *nanoseconds*, not the *microseconds* mentioned in your title. Microseconds would be *six* digits of decimal fraction. – Basil Bourque May 18 '17 at 06:23
  • How did 00 hours turn into 12 hours? And why is 12 noon labeled "AM"? – Basil Bourque May 18 '17 at 06:31
  • DateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS aa"); I was using like this. missed to add aa in the end while posting here – Ranjith Reddy May 18 '17 at 06:36
  • Again I ask: How is twelve noon "AM"? And why did hours change from 00 to 12? – Basil Bourque May 18 '17 at 06:42
  • First I have 00(HH) in my input that means midnight 12 which is nothing but AM. and by seeing 00 you should know my input is in 24 hour clock @BasilBourque – Ranjith Reddy May 18 '17 at 06:47
  • @BasilBourque doesn't 00 become 12 when you change from 24 hour to 12 hour format? – Ray May 18 '17 at 06:51
  • @Ray No, not in the [ISO 8601](https://en.m.wikipedia.org/wiki/ISO_8601) style. Twelve-hour clock runs from 0…12 for morning and 12-1…11-0 for afternoon and evening with new day starting at 0. Twenty-four hour clock runs 0-23 hours with a new day starting at 0. In either clock, the only 12 is noon. – Basil Bourque May 18 '17 at 07:09
  • 2
    @BasilBourque It was never noon. 00 in the 24-hour clock and 12am in the 12-hour clock both refer to midnight. This aspect of the question is perfectly correct. The 12-hour clock runs from 01 to 12 in both halves of the day, and does not have any such thing as 00. – Dawood ibn Kareem May 19 '17 at 05:47

1 Answers1

2

You are using the troublesome old legacy date-time classes that are now supplanted by the java.time classes. These legacy classes are limited to milliseconds which means three decimal places in the fraction of a second. So the legacy classes cannot handle the nine digits you desire.

The nine digits of decimal fraction you want means a resolution of nanoseconds rather than milliseconds. Fortunately, the java.time classes have a resolution of nanoseconds.

Parse your input as a LocalDateTime object as it lacks any indication of time zone or offset-from-UTC. Replace the SPACE in middle with a "T" to comply with standard ISO 8601 format.

String input = "2017-05-17 00:31:16.227".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input );

You should be able to force the trailing zeros you desire in the decimal fraction in a string representing the value of this LocalDateTime object. Use either a DateTimeFormatter or a DateTimeFormatterBuilder.

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