-5

I have a date like "Sun Apr 01 01:00:00 EEST 2018" and I want to convert it to the following numeric format: Day-Month-Year Hours:Minutes:Seconds

I want to do that in Java. Can anybody help?

Thank you a lot

azro
  • 53,056
  • 7
  • 34
  • 70
Manos Kardaris
  • 45
  • 1
  • 2
  • 8
  • 1
    have you tried anything? – Rahul B Jul 13 '18 at 12:07
  • a format is only a String, the date wn't change, what have you tried so far ? – azro Jul 13 '18 at 12:10
  • 1
    The problem with this duplicate is that the majority will stop after reading java7 part, and won't go to java 8 update – azro Jul 13 '18 at 12:13
  • When you say you “have a date”, do you mean a `java.util.Date`? Also asking because that class is long outdated and poorly designed, you should see if you can get a `LocalDate` or a `ZonedDateTime` instead, for example. – Ole V.V. Jul 13 '18 at 12:32
  • I am trying to decide whether to vote for closing this question as too broad since you appear to have done no attempt and it’s not the job of Stack Overflow to teach you the basics; or rather as a duplicate since this question has been asked and answered many times before. In any case: use your search engine, but ignore the pages endorsing `SimpleDateFormat` because that class is notoriously troublesome, and 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. Jul 13 '18 at 12:36
  • Would [my answer here](https://stackoverflow.com/a/47819620/5772882) be helpful? – Ole V.V. Jul 13 '18 at 12:46
  • 1
    @OleV.V. Vote as duplicate if you know one, and down-vote if you feel like it is justified (both actions don't exclude themselves). But lets not treat lack of research as reason for closure. IMO the simpler question is, the better, and it may also not require backstory about what research OP did. Questions like "How to do ...[some specific, not very broad thing]?" are very useful to others which is main goal of this site. We can answer them reasonably, often by using different ways which later can be voted on to decide what people like and which should be avoided. – Pshemo Jul 13 '18 at 12:55

1 Answers1

2

I would use the DateTimeFormatter for this. You can find more information in the docs. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

However, here an example:

DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
LocalDateTime date = LocalDateTime.parse("Sun Apr 01 01:00:00 EEST 2018", parser);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(date)); //01-04-2018 01:00:00

The Locale.US part is required even if you don't live in the US, otherwise it may not be able to parse Sun to Sunday and Apr to April an so on. Probably it works with Locale.UK to and more but for example it didn't work for me without because i live in switzerland.

Alexander
  • 54
  • 5