1

I am a little confused about what I see with the following test code:

public class TheDateIssue {

    public static void main(String[] args) {
        String TIME_FORMAT = "hh:mm aa";
        try {
            Date theDate = new SimpleDateFormat(TIME_FORMAT).parse("13:15 pm");
            System.out.println("theDate: " + theDate);
        } catch (ParseException e) {
            System.out.println("Exception while converting string to Date. \n" + e.getLocalizedMessage());
        }
    }

The time to be parsed is "13:15 pm" - but the sysout output lists the time (highlighted below). I was expecting either 13:15:00 PST or 01:15:00 pm PST

theDate: Fri Jan 02 **01:15:00 PST** 1970

What am I doing wrong? :(

S P
  • 101
  • 2
  • 13
  • 2
    Use Java's `LocalDateTime` or `LocalTime` if that is all you need. – SedJ601 Jul 19 '21 at 21:00
  • 1
    You're printing using Date's [toString()](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toString--) method. You probably meant to [use the format() method](https://stackoverflow.com/a/28631350/12764490) of the `SimpleDateFormat`. – Tim Hunter Jul 19 '21 at 21:10
  • Got it! - I will convert using SimpleDateFormat before printing/using it :) - Thanks, Tim Hunter :) But when I changed to formatter.format(theDate) - using the same TIME_FORMAT, it shows "theDate: 01:15 AM" – S P Jul 19 '21 at 21:14
  • @SP No problem. Those systems are outdated though, you may want to check out [LocalTime](https://howtodoinjava.com/java/date-time/java-localtime/) and [DateTimeFormatter](https://howtodoinjava.com/java/date-time/java8-datetimeformatter-example/) for this. – Tim Hunter Jul 19 '21 at 21:19
  • 2
    What time is "13:15 pm" supposed to be anyway? Isn't the AM/PM an indicator for the 12-hour clock system? How does "13:15" fit into this system? Wouldn't that make it an invalid input anyway? – Progman Jul 19 '21 at 21:32
  • 1
    **Why** are you still using obsolete `Date` and `SimpleDateFormat`? They are troublesome, and they lead to problems you described. Like Sedrick, Tim Hunter and H3AR7B3A7 already mentioned, you should use classes from the `java.time` package. Which one(s) depends on your needs. – MC Emperor Jul 19 '21 at 22:27
  • `13:15 pm` seems redundant to me. Since the hour is 13, we already know that the time is in PM. Are you sure that you are getting such a redundant time string? Would you get 12 midnight as `00:00 am` or `12:00 am`? – Ole V.V. Jul 22 '21 at 18:38

1 Answers1

1

Try this:

LocalTime time = LocalTime.parse("13:15")
System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm a")));

Or:

ZonedDateTime time = ZonedDateTime.of(LocalDate.now(), LocalTime.parse("13:15"), ZoneId.of("America/Los_Angeles"));
System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm:ss zz")));

And if for some reason you are stuck with a date class, transform it somehow:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ssZ aa");
Date theDate = simpleDateFormat.parse("13:15:00 PST PM");
ZonedDateTime date = ZonedDateTime.ofInstant(theDate.toInstant(), ZoneId.of("America/Los_Angeles"));
System.out.println(date.format(DateTimeFormatter.ofPattern("HH:mm:ss zz")));

But you are right, it is weird behaviour. There are good reasons they created LocalDate etc, moving away from the Date class.

H3AR7B3A7
  • 4,366
  • 2
  • 14
  • 37