1

I have following code to format a date, but the output does not match with the pattern.

try{
        String date = "2014-11-1T12:14:00";
        Date convertedDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        System.err.println(convertedDate.toString());
        }catch(ParseException p){
            System.err.println(p.getMessage());
        }

Output

Sat Nov 01 00:00:00 EST 2014
Jack
  • 6,430
  • 27
  • 80
  • 151
  • So you don't know whether it's the output or the exception handler that is printing... – John3136 Oct 13 '14 at 01:46
  • read this post - http://stackoverflow.com/questions/7150231/simpledateformat-string – Erran Morad Oct 13 '14 at 01:47
  • 1
    `Date` has no concept of format, it is only a container for the number of milliseconds since the Unix Epoch. Instead, you need to format the `Date` value to a `String` using the desired format... – MadProgrammer Oct 13 '14 at 01:49

2 Answers2

1

I think you are questioning why you don't get the time, that's because your format doesn't include it. I suggest you use "yyyy-MM-dd'T'HH:mm:ss" to match your input and "yyyy-MM-dd" when you call DateFormat#format(Date),

try {
  String date = "2014-11-1T12:14:00";
  Date convertedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(date);
  System.out.println(convertedDate.toString());
  // Per your comment you wanted to `format()` it like -
  System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(convertedDate));
} catch (ParseException p) {
  System.err.println(p.getMessage());
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

For what you described below in our conversation, you can simply do this:

System.err.println((new SimpleDateFormat("yyyy-MM-dd")).format(convertedDate));
Peter Pei Guo
  • 7,770
  • 18
  • 35
  • 54