2

I have a date string as "1/10/24 7:00 PM" (10th Jan.2024). How to parse it using SimpleDateFormat?

String date_time = "1/10/24 7:00 PM";
Instant answer;
try {
    answer = Instant.parse(date_time);
} catch(DateTimeParseException ex) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    answer = simpleDateFormat.parse(date_time).toInstant();
    System.out.println(answer);
}
5
  • 2
    Looks like that date isn't formatted according to the default pattern. Try and pass the correct pattern to the SimpleDateFormat constructor. Commented Jan 31, 2024 at 7:52
  • 2
    Stop using outdated classe like SimpleDateFormat use The DateTimeFormatter of the java.time API Commented Jan 31, 2024 at 7:52
  • 1
    You're already using the new java.time API (e.g. Instant), why do you want to revert to the older and more error prone old one based on java.util.Date (e.g. SimpleDateFormat) ? Commented Jan 31, 2024 at 7:53
  • 1
    simpleDateFormat.parse(date_time , new java.text.ParsePosition(0)) you are using an extremely old and flawed "Date" API , use java.time API "since Sun/Oracle 1.8.0 (open 11)" . parse position is only a placeholder for the internal method workings. Commented Jan 31, 2024 at 9:36
  • It’s not quite as complicated as you seem to think: Instant answer = LocalDateTime .parse(date_time, DateTimeFormatter.ofPattern("M/d/uu h:mm a", Locale.ENGLISH)) .atZone(ZoneId.systemDefault()) .toInstant();. <in my time zone it gave an Instant of 2024-01-10T18:00:00Z. Commented Feb 14, 2024 at 20:01

1 Answer 1

4

I have a date string as "1/10/24 7:00 PM" (10th Jan.2024). How to parse it using SimpleDateFormat?

The java.util date-time API and their corresponding parsing/formatting type, SimpleDateFormat are outdated and error-prone. In March 2014, the modern Date-Time API supplanted the legacy date-time API. Since then, it has been strongly recommended to switch to java.time, the modern date-time API.

The given date-time string does not have time zone information; therefore, parse it into a LocalDateTime and then apply the system-default time-zone to convert it into a ZonedDateTime. Finally, convert the ZonedDateTime into an Instant.

Demo:

public class Main {
    public static void main(String[] args) {
        String strDateTime = "1/10/24 7:00 PM";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/uu h:mm a", Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
        ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault());
        Instant output = zdt.toInstant();
        System.out.println(output);
    }
}

Output:

2024-01-10T19:00:00Z

ONLINE DEMO

Note that you can use Instant#parse only for those strings which conform to DateTimeFormatter.ISO_INSTANT e.g. Instant.parse("2011-12-03T10:15:30Z").

Learn about the modern date-time API from Trail: Date Time

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.