0

I need to format a String that looks like this:

"2018-07-20 18:53:46.598000 +02:00:00" 

into a DateTime object like this:

20/07/2018 (HH with Timezone applied):53:46

My approach has been:

String dateTimePattern = "dd/MM/yyyy HH:mm:ss";
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(dateTimePattern);                                                                                                                                                         
Date feUltModDateTime = dateTimeFormat.parse(feUltMod);
feUltMod = feUltModDateTime.toString();

But I'm getting a parse error:

java.text.ParseException: Unparseable date: "2018-07-20 18:53:46.598000 +02:00:00"
LoloSSS
  • 91
  • 2
  • 12
  • What is `feUltMod` ? – Pramod Aug 17 '18 at 11:36
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 17 '18 at 11:46
  • Possible duplicate of [String to Date Conversion mm/dd/yy to YYYY-MM-DD in java](https://stackoverflow.com/questions/50405211/string-to-date-conversion-mm-dd-yy-to-yyyy-mm-dd-in-java) and/or of [Parsing from String to Date throws Unparsable Date Error](https://stackoverflow.com/questions/50514017/parsing-from-string-to-date-throws-unparsable-date-error) – Ole V.V. Aug 17 '18 at 11:47

2 Answers2

2

java.time

    DateTimeFormatter origFormatter 
            = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSS XXXXX");
    DateTimeFormatter desiredFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
    ZoneId desiredZone = ZoneId.of("America/Fort_Nelson");

    String feUltMod = "2018-07-20 18:53:46.598000 +02:00:00";
    OffsetDateTime dateTime = OffsetDateTime.parse(feUltMod, origFormatter);
    ZonedDateTime dateTimeWithTimeZoneApplied = dateTime.atZoneSameInstant(desiredZone);
    feUltMod = dateTimeWithTimeZoneApplied.format(desiredFormatter);
    System.out.println(feUltMod);

Output from this snippet is:

20/07/2018 09:53:46

Generally you need two formatters for converting a date or date-time from one format to another: one that specifies the format to convert from and one that specifies the format to convert to.

into a DateTime object like this

A date-time object doesn’t have a format, so in that respect cannot be “like this”. dateTimeWithTimeZoneApplied in the above snippet is in the specified time zone, so has the hours adjusted. After converting to this time zone I have formatted into a string in the format you mentioned, in case you wanted this (I didn’t find it clear).

I am using and recommending java.time, the modern Java date and time API. The date and time classes you were using, Date and SimpleDateFormat, are long outdated and poorly designed, it’s not worth struggling with them. Also SimpleDateFormat supports only milliseconds so can only work correctly with exactly 3 decimals on the seconds, not with the 6 decimals you have got.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date df = new Date();
    String yourString = sdf.format(df);

    Date parsedDate = sdf.parse(yourString);
    Timestamp sqlDate = new java.sql.Timestamp(parsedDate.getTime());

The above code will give you current Timestamp.Timestamp will provide better feasibilty

Dhanraj
  • 1,162
  • 4
  • 18
  • 45
  • I fail to see how this might answer the question. Your code just gave `2018-08-17 13:20:00.0` on my computer, not the `20/07/2018 (HH with Timezone applied):53:46` that was asked for. Also you should avoid the outdated and poorly designed date-time classes `SimpleDateFormat`, `Date` and `Timestamp`. – Ole V.V. Aug 17 '18 at 12:15
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 17 '18 at 22:18