0
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class myTests {

    public static void main(String[] args) {
        DateFormat formatter = new SimpleDateFormat("YYYY-MMM-dd", Locale.US); 
        try {
            Date sortingDate = (Date)formatter.parse("2017-Jul-13");
            System.out.println("Sorted Date is:"+sortingDate);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Result is

Sorted Date is:Sun Jan 01 00:00:00 PST 2017

Why wont it show date i gave 2017 Jul 13 Can you please let me know.

Thanks Abe

Jens
  • 67,715
  • 15
  • 98
  • 113

1 Answers1

7

uppercase Y is Week year. What you Need is lowercase y = Year.

So Change new SimpleDateFormat("YYYY-MMM-dd", Locale.US); to new SimpleDateFormat("yyyy-MMM-dd", Locale.US); and you should get the correct result.

For more informations see the javadoc of SimpleDateFormat

If you are using java8, you should Change to DateTimeFormatter and the new DateTime API

Jens
  • 67,715
  • 15
  • 98
  • 113