0

I am currently facing a problem when it comes to some Java methods that weren't explained to me in lectures very well. I need to write a program that accepts user-inputted strings (particularly a date) in yyyymmddhhss format, which should then convert to hh:mm Month day, year.

E.g. 201901151500 outputs: "03:00 PM January 15, 2019". 

Currently, in my program, I have accepted the user's input and implemented a method that returns an error message if the inputted format is invalid.

Any tips on where to go from here? Advice is greatly appreciated-- thank you!

YCF_L
  • 49,027
  • 13
  • 75
  • 115
John B
  • 49
  • 5
  • 2
    Have a look at the [`LocalDateTime`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalDateTime.html) and [`DateTimeFormatter`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html) classes in the [`java.time`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) and [`java.time.format`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/package-summary.html) packages. – David Conrad Mar 25 '19 at 21:00
  • 1
    [Search Stack Overflow](https://duckduckgo.com/?q=site%3Astackoverflow.com+java+yyyymmddhhss&t=osx&ia=web) before posting. – Basil Bourque Mar 26 '19 at 04:58
  • Welcome to Stack Overflow. We generally love to see a little code, so when you have already written a program that you now need to extend, please show it. We can guide you much better when we can see what your starting point is! – Ole V.V. Mar 26 '19 at 08:52

2 Answers2

4

If you are using Java 8 you can use java.time API like so :

String input = "201901151500";
LocalDateTime dt = LocalDateTime.parse(input, DateTimeFormatter.ofPattern("uuuuMMddHHmm"));
String output = dt.format(DateTimeFormatter.ofPattern("hh:mm a MMMM dd, uuuu")); 
>> output = 03:00 PM janvier 15, 2019
YCF_L
  • 49,027
  • 13
  • 75
  • 115
  • 1
    Concise, fast and simple! Did I say fast? :-) – MS90 Mar 25 '19 at 21:05
  • 1
    *FYI:* The correct pattern symbol for year is `uuuu`, since the patterns don't specify an *era* (`G`). – Andreas Mar 25 '19 at 21:07
  • 2
    @Andreas I search for the meaning and I found your helpful answer [here](https://stackoverflow.com/a/41178418/5558072), already up-vote that nice answer, thank you ;) – YCF_L Mar 25 '19 at 21:12
  • @JustAFellowCoder I don't ask up-votes, I mean that I already up-vote the answer of Andreas [here](https://stackoverflow.com/questions/41177442/uuuu-versus-yyyy-in-datetimeformatter-formatting-pattern-codes-in-java/41178418#41178418) ;( – YCF_L Mar 25 '19 at 21:13
  • @Andreas even if you don't specify it, you can still technically use yyyy right? – JustAFellowCoder Mar 25 '19 at 21:13
  • @YCF_L nice save ;) kidding. Sorry misread it. – JustAFellowCoder Mar 25 '19 at 21:15
  • 3
    @JustAFellowCoder Yes, you can still *technically* use `yyyy`, if you are absolutely 100% guaranteed that years will never ever be less that year 1. Since you don't normally think about it, it's better to get into the habit of always using `uuuu`, so you don't forget the few times it actually makes a difference. If you read [my answer](https://stackoverflow.com/a/41178418/5221149), which YCF_L linked to, you'd see that referred to as [*defensive programming*](https://en.wikipedia.org/wiki/Defensive_programming). – Andreas Mar 25 '19 at 21:18
  • 1
    Thank you for your help! – John B Mar 26 '19 at 13:27
1

Use the DateTimeFormatter as defined here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Pay attention to the parse method. You can define a formatter that takes in a string and then returns it in a certain way, almost any way you choose.

Here is the LocalDataTime class: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html

Example code:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
LocalDateTime local = LocalDateTime.parse("2004 12 25", formatter);
JustAFellowCoder
  • 294
  • 1
  • 11