You could build a DateTimeFormatter
that parses case insensitively and uses an English Locale
along with a matching pattern, because your representation of the month is not parseable by a pattern only.
See the following example where every step is done explicitly and where UTC is used as time zone. Alternatively, you can use the time zone of the system by replacing ZoneId.of("UTC")
with ZoneId.systemDefault()
, which will affect the output, of course, if the system's time zone is not UTC. I chose UTC here to have comparable output since I don't know your time zone (do you?):
public static void main(String[] args) {
// example input
String source = "30-JUL-21";
// create a formatter that parses case-insensitively using a matching pattern
DateTimeFormatter caseInsensitiveDtf = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-uu")
.toFormatter(Locale.ENGLISH);
// parse the String using the previously defined formatter
LocalDate localDate = LocalDate.parse(source, caseInsensitiveDtf);
// print this intermediate result
System.out.println(localDate);
// build up a datetime by taking the start of the day and adding a time zone
ZonedDateTime zdt = localDate.atStartOfDay(ZoneId.of("UTC"));
// print that intermediate result, too
System.out.println(zdt);
// then simply convert it to an Instant
Instant instant = zdt.toInstant();
// and print the epoch millis of it
System.out.println(instant.toEpochMilli());
}
The output of it is this (last print uses the resulting Instant
):
2021-07-30
2021-07-30T00:00Z[UTC]
1627603200000