-2

My program reading data from a database and I want to convert some fields to date format (date and time separately). I can do this from sql query but is it possible to do from java code.

date format in table = MMDDHHMISS (month day hour minute sec)

and these are sql queries now I'm using-

TO_CHAR(TO_DATE(DATETIMECOLUMN, 'MMDDHH24MISS'),'DD/MM/YYYY') AS MY_DATE

TO_CHAR(TO_DATE(DATETIMECOLUMN, 'MMDDHH24MISS'),'HH24:MI:SS') AS MY_TIME

Thanks in advance!

Sameera.San
  • 67
  • 2
  • 15
  • https://stackoverflow.com/a/4216767/5613292 – Mehdi Javan Sep 07 '17 at 12:19
  • 3
    Possible duplicate of [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – baao Sep 07 '17 at 12:19
  • I went trough above answers but couldn't find a way to convert my kind of String to date time – Sameera.San Sep 07 '17 at 12:26
  • This has been asked and answered several time before. Please search harder. – Ole V.V. Sep 07 '17 at 12:31
  • The best you can do is to avoid converting your database date-time to string and back. Better to get for example `LocalDateTime` objects directly from your result set. – Ole V.V. Sep 07 '17 at 12:33
  • Sorry about the off-StackOverflow link, it’s in here if you scroll down to it: [Convert Java Date/Time to String & String back to Date/Time](https://www.java-success.com/convert-java-date-to-string-string-back-to-date/). – Ole V.V. Sep 07 '17 at 12:37
  • Please see [this Stack Overflow answer](https://stackoverflow.com/a/38561397/5772882) for how to get `LocalDateTime` objects from your result set. – Ole V.V. Sep 07 '17 at 12:40
  • 1
    Should your resulting date include a year? If so, how to decide which year? Java does offer the `MonthDay` class for dates without a year, in case that fulfils your requirements. – Ole V.V. Sep 07 '17 at 12:44
  • Please see these questions: (1) [Java - How to check if a value is of type Date](https://stackoverflow.com/questions/43631718/java-how-to-check-if-a-value-is-of-type-date). (2) [Android converting date time parse error](https://stackoverflow.com/questions/44487282/android-converting-date-time-parse-error-even-tried-joda-time) (3) [Result of parsing date is wrong by 16 hours 2 minutes](https://stackoverflow.com/questions/42884951/result-of-parsing-date-is-wrong-by-16-hours-2-minutes). – Ole V.V. Sep 07 '17 at 12:53

1 Answers1

2

Here’s a suggestion:

    DateTimeFormatter databaseStringFormatter = DateTimeFormatter.ofPattern("MMddHHmmss"); 
    String sampleDatabaseString = "1129225145";
    MonthDay myDate = MonthDay.parse(sampleDatabaseString, databaseStringFormatter);
    LocalTime myTime = LocalTime.parse(sampleDatabaseString, databaseStringFormatter);
    System.out.println("Date: " + myDate + ". Time: " + myTime + '.');

The above prints

Date: --11-29. Time: 22:51:45.

MonthDay is a date without a year, useful for birthdays and other anniversary dates.

Unless there are specific reasons to avoid it, I think you should rather change the datatype of your database column to datetime or similar and then retrieve LocalDateTime objects from your result set as shown in this answer. This would free you from any conversion from string to date and time in either the database query or in Java.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161