0

I want to take a date value as String in the format yyyy-MM-dd and return a jabva util date in the same format. for this I am using below code , but the result is coming as "Wed Sep 18 00:00:00 CEST 2013"

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String  strdate = "2013-09-18";
    Date utilDate = sdf.parse(strdate);
    System.out.println(utilDate);

Do i need to consider the locale also ? Please help to achieve this. Thanks in advance

Sambuddha
  • 245
  • 5
  • 18
  • 1
    The result of `sdf.parse(strdate)` is just a string; assigning it to `utilDate` parses the string into a new `Date` object. At no point is the formatting information transferred. – MTCoster Nov 14 '18 at 17:09
  • You format the String to a Date but not the output back to a String in the wanted format – csalmhof Nov 14 '18 at 17:10
  • 1
    Simply do `System.out.println(sdf.format(utilDate));` – Thomas Fritsch Nov 14 '18 at 17:17
  • 2
    You might want to consider switching to LocalDate and DateFormatter classes from Java 8. It is the way to go. SimpleDateFormat is outdated – SamwellTarly Nov 14 '18 at 17:26
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 14 '18 at 17:47
  • 1
    use java8 DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd"); – GauravRai1512 Nov 14 '18 at 17:49
  • 2
    @GauravRai1512 That standard ISO 8601 format is used by default in the `LocalDate` class. No need to define a formatting pattern, no need to use `DateTimeFormatter`. Just use: `LocalDate.parse( "2018-01-23" )` – Basil Bourque Nov 14 '18 at 20:30

1 Answers1

2

Please try below code. You also have to format your simpleDateFormat object and pass utilDate object inside it to create formatted date as specified by you.

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

public class DateTest {

     public static void main(String[] args) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String  strdate = "2013-09-18";
            Date utilDate = sdf.parse(strdate);
            String date=sdf.format(utilDate );
            System.out.println(date);
 }
}

We can also implement through Java8 like below:

import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateTest {

     public static void main(String[] args) throws ParseException{
         String  strdate = "2013-09-18";
            DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(strdate, formatter);
            System.out.println(date.format(formatter));
}

}

or you can also use only LocalDate to change in java util like below.

import java.time.LocalDate;

public class TestCircle {

    public static void main(String args[]){
        String  strdate = "2013-09-18";
        LocalDate date = LocalDate.parse(strdate);
        System.out.println(date);
    }

}
GauravRai1512
  • 834
  • 6
  • 14