Sachika Sandeepa
Sachika Sandeepa

Reputation: 31

How to get date from date time in java

This is my date string,

String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";

I want to get only date with "yyyy-MM-dd" format. How can I get date with this format. I tried with below code, But it's not working.

String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";

          Date date = null;
        try {
            date = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss Z").parse(dd);
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
          String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
           System.out.println("\n"+newstr+"\n");

Upvotes: 1

Views: 4308

Answers (6)

Anonymous
Anonymous

Reputation: 86379

java.time

Using java.time, the modern Java date and time API, it’s pretty simple when you know how:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
            "MMM-dd-uuuu HH:mm:ss.SSS zzz(xx)", Locale.ROOT);
    String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
    LocalDate date = LocalDate.parse(dd, formatter);
    System.out.println(date);

Output is:

2019-11-08

As we can see, your string is in UTC. I have assumed that you also want your date in UTC. If not, we need to parse into a ZonedDateTime, convert to your desired zone and then format into your desired output format with only the date.

I assumed that UTC(+0000) is a time zone abbreviation followed by an offset in brackets.

The date and time classes that you were trying to use, Date and SimpleDateFormat, are poorly designed and long outdated, the latter in particular notoriously troublesome. You should not use them. java.time is so much nocer to work with.

What went wrong in your code?

  • MM is for two-digit month number, for example 11 for November or 08 for August. For month abbreviation like Nov you need MMM.
  • Z is for offset like +0000. While this does appear in your string, the text UTC comes before it. Edit: Confusingly SimpleDateFormat parses UTC as time zone and then ignores the remainder of the string. I for my part shouldn’t want to rely on this behaviour.
  • I recommend you specify a locale with your formatter to control which language it expects in the string. With MMM for month abbreviation, if you rely on the default locale and it happens to be Welsh, your formatter will expect Tach for November.

Links

Upvotes: 3

Ryuzaki L
Ryuzaki L

Reputation: 40078

Don't use SimpleDateFormat and Date classes those are legacy, Use java-8 modern date time API classes, First create DateTimeFormatter with the input format date

String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM-dd-uuuu HH:mm:ss.SSS zzz(Z)");

And then use OffsetDateTime to parse it with particular offset (In your case UTC)

    OffsetDateTime dateTime = OffsetDateTime.parse(dd,formatter);

And then get the LocalDate

    System.out.println(dateTime.toLocalDate());

Upvotes: 1

Alfred Luu
Alfred Luu

Reputation: 2026

"Nov" is known as "MMM" format so it will throw the exception. Try this code: https://onlinegdb.com/H1keFI5oS

import java.util.Date;
import java.text.SimpleDateFormat;

public class Main
{
     public static void main(String []args){
        String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";

        Date date = null;
        try {
            date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").parse(dd);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
        System.out.println("\n"+newstr+"\n");
     }
}

Upvotes: 0

Your input string is

 String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";

You want is

yyyy-MM-dd

first it is not possible you should be getting yyyy-MMM-dd for that the code will be

String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";

          Date date = null;
        try {
            date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss Z").parse(dd);
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
          String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
           System.out.println("\n"+newstr+"\n");

Upvotes: 0

Gaurav Dhiman
Gaurav Dhiman

Reputation: 1023

    String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";

    Date date = null;
  try {
      date = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss.S z").parse(dd);
  } catch (ParseException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
  }
    String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
     System.out.println("\n"+newstr+"\n");

This might help you.

Upvotes: 0

ASK
ASK

Reputation: 1274

The pattern you provide in the SimpleDateFormatter class constructor should match with the provided output. e.g. since you are providing month as three letters, use 3 M's in the pattern.

String dd = "Nov-08-2019 07:00:28";

    Date date = null;
  try {
      date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").parse(dd);
  } catch (ParseException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
  }
    String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
     System.out.println("\n"+newstr+"\n");
}

Upvotes: 0

Related Questions