-1

I want to convert a string date object to Date object. In my request there is a field of the form "dateCreated":"Tue Jul 30 13:41:40 WIB 2019". I need to model it to Date class. How can I achieve this?

I thought the controller will automatically cast it to Date object. It didn't work. Also, simple casting to Date object from string also didnt work. I could not find a string formatter for this also.

  private Date dateCreated = new Date();

So I want to convert a stringified Date object to Date object.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Harshit Gupta
  • 81
  • 1
  • 1
  • 7
  • 1
    This looks similar to this stackoverflow question. https://stackoverflow.com/questions/28807166/indonesian-times-in-simpledateformat – Arun Gopinathan Jul 30 '19 at 18:35
  • 2
    I imagine there’s a duplicate out there, but either way, do you have to use `Date`, could you not use `LocalDate`? – achAmháin Jul 30 '19 at 18:40
  • 1
    I recommend you don't use `Date`. That class is poorly designed and long outdated. Use `Instant` or `ZonedDateTime`. And a `DateTimeFormatter` for parsing the string. – Ole V.V. Jul 30 '19 at 19:55
  • I am migrating an already existing application to Java, so don't have much flexibility there. All the old data is stored that way. – Harshit Gupta Jul 31 '19 at 02:57

1 Answers1

0

If you describe your format then you can do it with Joda time

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);

Well a slightly more complex problem of how do you parse date that can be in multiple formats, can be solved

DateTimeParser[] parsers = { 
        DateTimeFormat.forPattern("yyyy-MM-dd HH").getParser(),
        DateTimeFormat.forPattern("yyyy-MM-dd").getParser() };
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();

DateTime date1 = formatter.parseDateTime( "2010-01-01" );
DateTime date2 = formatter.parseDateTime( "2010-01-01 01" );

https://www.joda.org/joda-time/

edit

From java 8 you can use

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date1 = LocalDate.from(formatter.parse("2010-01-01"));
Community
  • 1
  • 1
Margus
  • 19,694
  • 14
  • 55
  • 103
  • Additionally, you can make it work with different time zones and culture. – Margus Jul 30 '19 at 18:42
  • 3
    Why joda though? That stuff is part of Java since 8 and there is a backport for 7. – Zabuzard Jul 30 '19 at 18:43
  • @Zabuza Just for info Joda-Time is what was migrated to java.time in java 8. – Margus Jul 30 '19 at 19:31
  • Margus, not exactly. The code you posted will not work in Java 8 since there has been a further development. – Ole V.V. Jul 30 '19 at 19:51
  • 1
    To clarify: The [*Joda-Time*](https://www.joda.org/joda-time/) project was an industry-leading effort at a robust well-thought-through framework for date-time handling. That project is led by Stephen Colebourne. He eventually used the lessons learned there to start anew with the *java.time* framework proposed to be built into Java 8 with [JSR 310](https://jcp.org/en/jsr/detail?id=310). In parallel Colebourne also led [*ThreeTen-Backport*](https://www.threeten.org/threetenbp/) to bring most of the *java.time* functionality to Java 6 & 7. Further adapted to early Android in *ThreeTenABP*. – Basil Bourque Jul 30 '19 at 20:00
  • @BasilBourque Thanks for the info. – Margus Jul 30 '19 at 20:34