-5

How can I get the current date and time and how can I transfer it into the following String format?

06-04-2020 05:00 PM

After that I need to add few minutes to the date? Please help!

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    What have you tried so far? – cegredev Apr 06 '20 at 11:23
  • 1
    How have you tried to get it? What went wrong? Any error or empty output? Can you show us your code? – deHaar Apr 06 '20 at 11:23
  • 1
    have you done any search on this site? – jhamon Apr 06 '20 at 11:25
  • And please add what's exactly meant by *get ... in ... format*... Do you have to parse or output a datetime? – deHaar Apr 06 '20 at 11:26
  • dd-MM-yyyy HH:mm a – GolamMazid Sajib Apr 06 '20 at 11:27
  • 3
    You guys are so annoying with your downvotes. This is a valid and good question for java beginners! I would recommend looking up `SimpleDateFormat` for formatting the date into that string. Also, you can simply get the CURRENT date and time with a call like this: `Date d = new Date();`Hope it helps! – F_Schmidt Apr 06 '20 at 11:30
  • Hi Guys, I have tried simpledateformat, calendar as well as local date. However, the point is I need to add few minutes to the date and I need in the desire format mentioned above. – Raghavendra Apr 06 '20 at 11:36
  • @F_Schmidt Thanks for your kind concern! I have managed to get it now! – Raghavendra Apr 06 '20 at 12:16
  • 1
    @F_Schmidt I didn’t downvote, but this question certainly deserves downvotes for lack of shown search and research effort. I agree that a constructive criticism in a comment is better than a downvote (or one may provide both). – Ole V.V. Apr 06 '20 at 12:55

2 Answers2

4

Your answer should look something like this:

String dateTimePattern = "dd-MM-yyyy HH:mm a";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimePattern );
LocalDateTime yourDay= LocalDateTime.of(2020, 4, 6, 17, 00);
System.out.println(formatter.format(ZonedDateTime.of(yourDay, ZoneId.of("UTC-5"))));

Please look this up for more info on the available formats.

PaianganuOm
  • 254
  • 1
  • 10
2
Date yourDate = new Date();  
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy h:m a");  
String formatedDate= sdf.format(yourDate);
mohit
  • 88
  • 9
  • 3
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Apr 06 '20 at 12:53