-2

A bunch of our sql migration files are named by the convention "V201801011200__migration.sql", and I've been assigned to write a java method to pick the latest migration. I want to do so by parsing the "201801011200" into a Java Datetime (or other similar object if this string's format isn't supported by Datetime).

How would I go about doing that? Please note that I am unable to rename these migration files into "2018-01-01-12:00", etc.

P_equals_NP_2021
  • 627
  • 1
  • 9
  • 27
  • 3
    Just because there are no dashes or colons doesn't mean they're not formatted. They're just formatted without dashes or colons. – Joe C Apr 10 '18 at 22:02
  • That is, assuming they are all numbered the same way. – Tripp Kinetics Apr 10 '18 at 22:27
  • 2
    Why would you need to parse the date to find the latest? In the given format (`yyyyMMddHHmm`), the text itself is ordered same as dates, i.e. a later date is a later alphabetic text. So just find the file with the last alphabetical filename. Parsing the date is overkill. – Andreas Apr 10 '18 at 22:28
  • Apart from just sorting the file names a couple of options: (1) Parse into a `LocalDateTime` using a `DateTimeFormatter.ofPattern("'V'uuuuMMddHHmm__'migration.sql'")`. (2) Pick out the date-time from the file name using a regexp and then parse with a date time formatter that does not include `V` and `migration.sql`. – Ole V.V. Apr 11 '18 at 03:59

2 Answers2

1

A simple string-to-time would be:

String timeString="201801011200";
SimpleDateFormat timeStringFormat = new SimpleDateFormat( "yyyyMMddHHmm" ) ;
Date time = timeStringFormat.parse(timeString);
System.out.println(time) ;
Marvin
  • 598
  • 5
  • 14
  • 2
    Please don’t teach the young ones to use `SimpleDateFormat`. That class 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/) and its `DateTimeFormatter` class. – Ole V.V. Apr 11 '18 at 03:52
  • Well not all are using Java 8 or latest technologies :) – Marvin Apr 11 '18 at 07:45
  • True, @Marvin. For Java 6 and 7, use [ThreeTen Backport](http://www.threeten.org/threetenbp/), it offers the `java.time` functionality. If you want to convey a Java 5 solution, very fine, but please tell us that that is what it is so no one gets fooled into thinking that this is the solution they should use with more modern Java versions. Please. – Ole V.V. Apr 11 '18 at 07:49
0
If you need java Date object use SimpleDateFormat:

SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmm");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm");
java.util.Date javaDate = inputFormat.parse("201801011200");
System.out.println("java.util.Date=" + javaDate);
System.out.println("output string=" + outputFormat.format(javaDate));

If you need just reformat string then use StringBuilder

String outputString = new StringBuilder("201801011200").
                insert(4, "-").
                  insert(7, "-").
                    insert(10, "-").
                      insert(13, ":").toString();
System.out.println("output string=" +outputString);
Vadim
  • 4,027
  • 2
  • 10
  • 26
  • 2
    Please don’t teach the young ones to use `SimpleDateFormat`. That class 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/) and its `DateTimeFormatter` class. – Ole V.V. Apr 11 '18 at 03:52
  • It still serves well if you know what java.util.Date is and how to properly work with it. So... where is your answer? – Vadim Apr 11 '18 at 16:28
  • 1
    There are fine `java.time` answers, for example [here](https://stackoverflow.com/a/4216767/5772882) and [here](https://stackoverflow.com/a/22180505/5772882). – Ole V.V. Apr 11 '18 at 16:32