4

I wanted to convert my string to LocalTime format

    String s = "1時30分:00";
    String ss = s.replace("時", ":").replace("分", ":");
    DateTimeFormatter timeColonFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("hh:mm a").toFormatter(Locale.JAPAN);
    System.out.println(timeColonFormatter);
    LocalTime colonTime = LocalTime.parse("3:30 am", timeColonFormatter);
    System.out.println(colonTime);

I received error:

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

Expected output:

3:30 AM

Selman Tosun
  • 428
  • 5
  • 14
Anonymous
  • 477
  • 3
  • 12
  • 1
    Is string `s` related to this in any way? – Sweeper Jul 03 '19 at 05:39
  • 1
    have you tried with "03:30" ? – Stultuske Jul 03 '19 at 05:39
  • I did the s for myself. Due to keep getting errors. I ended up inputing the input directly and differently. – Anonymous Jul 03 '19 at 05:53
  • Yes, I have tried "03:30" but still wont work – Anonymous Jul 03 '19 at 05:53
  • you miss `am-pm-of-day` in your string. It is part of the pattern – Jens Jul 03 '19 at 05:53
  • Yes but the problem here is when I include am or pm such as "03:30 am" and then at the pattern -> "hh:mm a". I still get the same error. – Anonymous Jul 03 '19 at 05:54
  • The `am-pm-of-day` is locale specific so i guess there must be japanese sign for that you have to use. if i Change locale to english than it works. Also see https://japanese.stackexchange.com/questions/44319/am-and-pm-in-japanese – Jens Jul 03 '19 at 05:58
  • Yes, I also realize about that. My current issue is I do not know what to assign into that empty space to replace am or pm in Locale.US. I tried to check the input from the documentation and it shows data type of .Short. I dont know what short data type to include. – Anonymous Jul 03 '19 at 05:59
  • In Locale.US it works perfectly fine without any problem since there are a lot of examples to refer to but not for Japanese version and other languages. – Anonymous Jul 03 '19 at 06:00
  • Why you want to replace the japanese characer with US characters? It should work also with the japanese character and Japan locale – Jens Jul 03 '19 at 06:05

1 Answers1

5

In your pattern you have a two-digit hour, and since your locale is Japan, you have to use the Japanese equivalent of AM/PM, which are 午前 / 午後, respectively, e.g.:

LocalTime.parse("03:30 午前", timeColonFormatter);
LocalTime.parse("03:30 午後", timeColonFormatter);

You can accept single-digits times, too, with the pattern "h:mm a".

Edit: You can also parse the Japanese time directly, without need to convert to semi-international format, e.g. using the pattern "h時mm分 a":

LocalTime colonTime = LocalTime.parse("3時30分 午前", timeColonFormatter);

Or, in the correct Japanese order, with the pattern "ah時mm分":

LocalTime colonTime = LocalTime.parse("午前3時30分", timeColonFormatter);
vlumi
  • 1,321
  • 1
  • 9
  • 18
  • Now I get understand. Sorry, am testing on many other languages so I didnt notice this simple mistake. Thank you so much sir – Anonymous Jul 03 '19 at 06:06
  • So there is no way to have it print AM after the string. – Anonymous Jul 03 '19 at 06:09
  • "AM" is "午前" in Japanese, which is used if you set the locale to `JAPAN`. – vlumi Jul 03 '19 at 06:11
  • 1
    Wow, this is even much more better. Thank you so much! – Anonymous Jul 03 '19 at 06:27
  • I wish to ask one final question. Is it possible to convert the output 03:30 --> 03:30:00. With its seconds set to 00? – Anonymous Jul 03 '19 at 06:40
  • What you are printing there is just the default formatting of `LocalTime`. You can format it any way you like, e.g. using the `DateTimeFormatter` created before: `timeColonFormatter.format(colonTime)`. You can also get the ISO time format "03:30:00": `DateTimeFormatter.ISO_TIME.format(colonTime)` – vlumi Jul 03 '19 at 06:48
  • i tried to use this somehow all gotten errors. .format will return back the original string. – Anonymous Jul 03 '19 at 06:58
  • 1
    Your `timeColonFormatter` will return the same format (it's used for both parsing and formatting), but the standard DateTimeFormatter.ISO_TIME should give you `hh:mm:ss`. – vlumi Jul 03 '19 at 08:23