-2

Tried converting string of yyyy-mm-dd, example 2013-12-30, to date object using SimpleDateFormat.parse("yyyy-mm-dd").

Expected output of 2013-12-30, received output of Mon Dec 30 00:00:00 EST 2013 object.

Tried finding out why SimpleDateFormat is returning a different format, but overwhelmed when trying to look through the java api. Asking for clarifications on what is going on and what would be a better approach.

Note: Stuck using java.utl.Date.

    ...
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    dateArray = new Date[rowCount];

    try {
        for(int index = 0; index < rowCount; index++){
            dateArray[index] = simpleDateFormat.parse(fileArray[index][0]);
            System.out.println(dateArray[index].toString());
        }
    } catch(ParseException err){
        System.out.println("ERR: Data parse exception. Format is not correct.");
        err.printStackTrace();
    }
  • 2
    If I may ask, why are you using the old `SimpleDateFormat` and `Date` classes? – MC Emperor Nov 16 '20 at 19:30
  • Instead of printing the `Date` directly, use your `SimpleDateFormat` (`format()` method) to get a string in that same format. – GriffeyDog Nov 16 '20 at 19:32
  • 1
    The format is separate from a `Date` object. Use `simpleDateFormat.format(dateArray[index]);`. – Kayaman Nov 16 '20 at 19:33
  • @MCEmperor: Taking an outdated college course on java. Lessons are updated, but not the Assignments. Only a few assignments have one has one or two specifications using deprecated packages. In this case it would be the Date class. – Navraj Bains Nov 16 '20 at 19:44
  • @Kayaman: Thank you for the clear explanation. I have a better understanding of how SimpleDataFormat works as well as slowing piecing together what I have been reading. I really should be building a print method in this case. Thank you. – Navraj Bains Nov 16 '20 at 19:48
  • Not necessarily, if you have that formatter as a local variable there. Might as well inline it, if you just want to print it. – Kayaman Nov 16 '20 at 19:49

1 Answers1

1

The pattern, mm stands for minute, not month. For month, you need to use MM.

Demo:

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

public class Main {
    public static void main(String[] args) throws ParseException {
        String strDate = "2013-12-30";
        SimpleDateFormat sdfISO8601 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdfISO8601.parse(strDate);
        System.out.println(date);

        String strDateISO8601 = sdfISO8601.format(date);
        System.out.println(strDateISO8601);

        // Some other format
        String strSomeOtherFormat = new SimpleDateFormat("EEEE MMM dd yyyy").format(date);
        System.out.println(strSomeOtherFormat);
    }
}

I also recommend you check Convert UTC String to UTC Date.

Note that the date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. I suggest you should stop using them completely and switch to the modern date-time API.

Using the modern date-time API:

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

public class Main {
    public static void main(String[] args) {
        String strDate = "2013-12-30";
        LocalDate date = LocalDate.parse(strDate);
        System.out.println(date);

        String strDate8601 = date.toString();
        System.out.println(strDate8601);

        // Custom format
        String customFormat = DateTimeFormatter.ofPattern("EEEE MMM dd uuuu").format(date);
        System.out.println(customFormat);
    }
}

Your date string is already in ISO 8601 format for date and therefore do not need to use any formatter to parse it when you use the modern date-time API.

Learn more about the modern date-time API at Trail: Date Time.

If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • @OleV.V. - I hope you are doing well. The first sentence `Tried converting string of yyyy-mm-dd, example 2013-12-30, to date object using SimpleDateFormat.parse("yyyy-mm-dd").` of the question is the real culprit . – Arvind Kumar Avinash Nov 16 '20 at 20:54