ZonedDateTime.parse not working for parsing time with am or pm

爱⌒轻易说出口 提交于 2020-04-15 01:24:40

问题


I am learning java, trying to build a tool to convert a specific time from timezone A to timezone B based on user input (input of the time, timezone A, and timezone B). This is about the part where the tool gathers a time in a specific format to convert it into a ZonedDateTime object.

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public static String fullTime;
public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm a");
public static ZonedDateTime newTime;

public static void getHourAndMinutes(){
        System.out.print("Please type in the time you have in mind in format hh:mm am/pm\n");
        Scanner in = new Scanner(System.in);
        fullTime = in.nextLine();
        System.out.println(fullTime);
        newTime = ZonedDateTime.parse(fullTime, formatter);

I have tried to enter the time in formats like 10:30PM, 10:30 PM, 10:30pm, 10:30 pm, 10:30p, 10:30 p, all of these entries has caused exception error to be thrown, I'm getting errors like this one

Exception in thread "main" java.time.format.DateTimeParseException: Text '10:30 pm' could not be parsed at index 6

Any idea what I might be doing wrong? Thanks!


回答1:


Since the user entering input is just representing time you need to parse it into LocalTime, and the other mistake is you are using the wrong pattern, H is hour-of-day (0-23); you need h in DateTimeFormatter

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");

LocalTime localTime = LocalTime.parse("10:30 PM",formatter);

After parsing the input into LocalTime you can convert it into ZonedDateTime. But you must specify a date (LocalDate) as well as the time and the zone. Your code in the Question had only the time-of-day, and lacked the date and zone needed to instantiate a ZonedDateTime.

ZonedDateTime dateTime = localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault());

And then you can convert it into another zone using withZoneSameInstant

ZonedDateTime result = dateTime.withZoneSameInstant(ZoneId.of("America/New_York));



回答2:


Any idea what I might be doing wrong? Thanks!

A number of things, I am afraid.

  1. For a ZonedDateTime you need a date and a time and a time zone. For parsing a string containing only time of day into a ZonedDateTime you would have needed to supply a default date and a default time zone. However, instead I would parse into a LocalTime, which is exactly a time if day without date and without time zone. After parsing you may convert. You need to decide a date for your conversion since your time zone A (and B too) probably uses a different UTC offset on different dates because of summer time (DST) and/or historic and/or future changes in their base UTC offset.
  2. You need to provide a locale for your formatter to tell it in which language to assume AM and PM. For example DateTimeFormatter.ofPattern("HH:mm a", Locale.ENGLISH).
  3. You need to enter AM or PM in the correct case (upper or lower case) for the locale you specified.
  4. For parsing a string with hour within AM or PM from 01 through 12 you need to use lower case hh in the format pattern string. Not upper case HH, which would be for hour of day from 00 through 23.


来源:https://stackoverflow.com/questions/61114302/zoneddatetime-parse-not-working-for-parsing-time-with-am-or-pm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!