Why SimpleDateFormat does not apply specified date pattern?

Jack

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
Elliott Frisch

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());
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why SimpleDateFormat does not apply specified date pattern?

From Dev

SimpleDateFormat date and time pattern

From Dev

Java SimpleDateFormat Pattern for JavaScript Date

From Dev

Why does SimpleDateFormat.format(Date) ignore the configured TimeZone?

From Dev

Why does SimpleDateFormat.parse accept an invalid date string?

From Dev

Why does SimpleDateFormat fail to parse weekday in date string?

From Dev

Why does SimpleDateFormat fail to parse weekday in date string?

From Dev

Why does Chromium apply different Japanese font than specified in settings?

From Dev

SimpleDateFormat with pattern yyyyMMddhhmmss unable to parse date "20160327020727"

From Dev

Why Calendar and SimpleDateFormat are showing a bad date?

From Dev

Does the ThreadPoolExecutor Apply the Template Pattern?

From Dev

Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

From Dev

Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

From Dev

Why SimpleDateFormat does not throw exception for invalid format?

From Dev

Why does this redirect apply?

From Dev

Why does this redirect apply?

From Dev

Why does this pattern not match?

From Dev

Why SimpleDateFormat accepts strings with no letters as valid date formats by design?

From Dev

What SimpleDateFormat Pattern to use?

From Dev

Java SimpleDateFormat: Pattern - ParseException

From Dev

Why does it apply the second argument?

From Dev

Why does this string not match the pattern

From Dev

Why does SimpleDateFormat.parse not throw an exception here?

From Dev

textarea, why does it keep scrolling if height is specified?

From Dev

Why does cp not copy a specified folder

From Dev

Why does this alignment attribute have to be specified in a typedef?

From Dev

Why the EventEmitter object does not emit the specified instance?

From Dev

Why does getent default to .station if no TLD is specified?

From Dev

Misbehaving of the date parsing in SimpleDateFormat

Related Related

  1. 1

    Why SimpleDateFormat does not apply specified date pattern?

  2. 2

    SimpleDateFormat date and time pattern

  3. 3

    Java SimpleDateFormat Pattern for JavaScript Date

  4. 4

    Why does SimpleDateFormat.format(Date) ignore the configured TimeZone?

  5. 5

    Why does SimpleDateFormat.parse accept an invalid date string?

  6. 6

    Why does SimpleDateFormat fail to parse weekday in date string?

  7. 7

    Why does SimpleDateFormat fail to parse weekday in date string?

  8. 8

    Why does Chromium apply different Japanese font than specified in settings?

  9. 9

    SimpleDateFormat with pattern yyyyMMddhhmmss unable to parse date "20160327020727"

  10. 10

    Why Calendar and SimpleDateFormat are showing a bad date?

  11. 11

    Does the ThreadPoolExecutor Apply the Template Pattern?

  12. 12

    Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

  13. 13

    Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

  14. 14

    Why SimpleDateFormat does not throw exception for invalid format?

  15. 15

    Why does this redirect apply?

  16. 16

    Why does this redirect apply?

  17. 17

    Why does this pattern not match?

  18. 18

    Why SimpleDateFormat accepts strings with no letters as valid date formats by design?

  19. 19

    What SimpleDateFormat Pattern to use?

  20. 20

    Java SimpleDateFormat: Pattern - ParseException

  21. 21

    Why does it apply the second argument?

  22. 22

    Why does this string not match the pattern

  23. 23

    Why does SimpleDateFormat.parse not throw an exception here?

  24. 24

    textarea, why does it keep scrolling if height is specified?

  25. 25

    Why does cp not copy a specified folder

  26. 26

    Why does this alignment attribute have to be specified in a typedef?

  27. 27

    Why the EventEmitter object does not emit the specified instance?

  28. 28

    Why does getent default to .station if no TLD is specified?

  29. 29

    Misbehaving of the date parsing in SimpleDateFormat

HotTag

Archive