-3

I've a datetime with value

"Mon Jan 12 06:20:06 IST 1976" 

How can I extract just the date from it?

12 Jan 1976

Thank you!

Hearen
  • 6,951
  • 2
  • 47
  • 57
Amrit Kr Lama
  • 101
  • 1
  • 10
  • 1
    You need to first search for existing questions my friend. I'm pretty sure that this question is already answered by a lot many people. – Eiston Dsouza Jul 20 '18 at 09:28
  • 1
    It is never the same date everywhere on Earth. Is it OK to get the date in the same time zone as in the string (whatever IST means, it’s ambiguous)? – Ole V.V. Jul 20 '18 at 10:11
  • Always search Stack Overflow thoroughly before posting. You can assume all the basic questions on date-time have already been asked and answered. – Basil Bourque Jul 21 '18 at 19:08
  • @EistonDsouza :) I stopped posting questions at all. – Amrit Kr Lama Apr 01 '19 at 06:47

1 Answers1

4

Using DateTimeFormatter in Java 8, you can easily do the parsing.

String s = "Mon Jan 12 06:20:06 IST 1976";
ZonedDateTime localDateTime = ZonedDateTime.parse(s, DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy"));

And you can generate String objects, automatically localized.

DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
DateTimeFormatter timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);

String date = dateFormatter.format(localDateTime.toLocalDate());
String time = timeFormatter.format(localDateTime.toLocalTime());

Jan 12, 1976

6:20:06 AM

Basil Bourque
  • 262,936
  • 84
  • 758
  • 1,028
Hearen
  • 6,951
  • 2
  • 47
  • 57