-1

I am working with system's API which returns me dates in this format - Sun Jun 10 05:23:03 2018.

I want to efficiently parse it to something that will look like this - "dd-mm-year".

Is there any parsing built in Java I can use? Or I need to use a specific function for it?

Thanks.

  • 2
    Use `LocalDate` and `DateTimeFormatter`. – daniu Jul 31 '18 at 14:43
  • [All you need is on the Java Documentation](https://docs.oracle.com/javase/tutorial/datetime/iso/format.html) and also [here](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns) is the list of all the Patterns `DateTimeFormatter` takes. – gtgaxiola Jul 31 '18 at 14:46
  • 1
    This duplicate question has answered using `java.time` package. – AxelH Jul 31 '18 at 14:48

2 Answers2

0

DateTimeFormatter

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss yyyy");
    System.out.println(LocalDate.parse(str, formatter));

Output:

2018-06-10

You can parse as below with SimpleDateFormat :

public static void main(String[] args) throws IOException, ParseException {
    String str = "Sun Jun 10 05:23:03 2018";
    DateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH);

    DateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy");

    Date date = fmt.parse(str);

    System.out.println(date);
    System.out.println(targetFormat.format(date));
}

Outputs :

Sun Jun 10 05:23:03 EET 2018
10-06-2018
Emre Savcı
  • 3,034
  • 2
  • 16
  • 25
  • It is a solution after all, LocalDateTime is part of java 8, what if before version 8? – Emre Savcı Jul 31 '18 at 14:59
  • I added the java 8 version.. – Emre Savcı Jul 31 '18 at 15:38
  • Much better like this! It was a solution, for a question asked today, we can expect at least Java 8 (except if specified), it is released since 2014. If you don't mention that for "newer" realese, there is a much more interesting solutions, this is incomplete. – AxelH Jul 31 '18 at 17:34
-1

You can use SimpleDateFormat class in Java. Following snippet will show you how to parse date accordingly.

Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String strDate= formatter.format(date);

I hope this is what you are looking for. I hope this helps you.

Pranali Rasal
  • 177
  • 2
  • 12
  • Most of the people are still on Java7 and Java8. And by outdated, do you mean its deprecated? Then as per i know, it's not at least not deprecated. – Pranali Rasal Jul 31 '18 at 14:55
  • And well, there is not restriction on class usage. – Pranali Rasal Jul 31 '18 at 14:56
  • If you're on Java 8, you should use the java.time API. – marstran Jul 31 '18 at 14:57
  • No, outdated as outdated... this API was not really intuitive, a lot needed to be known to be used correctly. This is why is it was "time" for an upgrade. I agree that this is still usable but for a recent question, this isn't a "good" question from my point of view. (especially from a student learning recent java feature ;) if not, I invite you to take a look at some Java 8 upgrade, there is some really nice feature to discover) (please, don't see this as criticism !) – AxelH Jul 31 '18 at 17:48
  • @AxelH, thankyou for the inputs, appreciate it. – Pranali Rasal Aug 01 '18 at 07:56