0
  1. I need to convert below String to java.util.Date String dtm = "2014-05-26T00:00:00Z";

  2. Then i need to convert this Date to java.sql.Date format like java.sql.Date sqlDate = (java.sql.Date) utilDate;

One more thing 1. I need to convert the below string to "2001-12-17T09:30+08:00" to java.util.Date and then i need to convert this in reversely

Please help me. i can only use Date object and should convert in this way (java.sql.Date) utilDate

Jam
  • 17
  • 5
  • http://stackoverflow.com/questions/530012/how-to-convert-java-util-date-to-java-sql-date?rq=1 this may help – Viet Jan 25 '17 at 04:32
  • A [possible example](http://stackoverflow.com/questions/33337487/localdate-how-to-remove-character-t-in-localdate/33337607#33337607), but since your format meets one of the iso formats, it might be better to use the format directly – MadProgrammer Jan 25 '17 at 04:34
  • Also duplicate of many Questions about parsing strings is standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, such as [this Question](http://stackoverflow.com/q/4525850/642706) and [this Question](http://stackoverflow.com/q/31090946/642706). – Basil Bourque Jan 25 '17 at 07:55

1 Answers1

0

Using SimpleDateFormatter you can achieve this.

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    String dateInString = "2014-05-26T00:00:00Z";

    try {

        Date utilDate = formatter.parse(dateInString.replaceAll("Z$", "+0000"));
        System.out.println(utilDate);

    } catch (ParseException e) {

    }

After getting you java.util.Date object you can convert it into java.sql.Date using follow way,

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

For more similar type of conversions of string into Date. see this

jack jay
  • 2,493
  • 1
  • 14
  • 27
  • Thanks a lot, can you please help me in this also 1. I need to convert the below string to "2001-12-17T09:30+08:00" to java.util.Date and then i need to convert this to string – Jam Jan 25 '17 at 05:59
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. `Instant.parse( "2014-05-26T00:00:00Z" )` – Basil Bourque Jan 25 '17 at 07:56