PermGenError
PermGenError

Reputation: 46408

SimpledateFormat's parse method java

This is a silly question, but I can't see what I am doing wrong. I want to parse a string like 24:00 to a date object. I am aware that if we use kk:mm as the date format the times would be from 01:00 to 24:00 (from the API). But I can't get the below code working.

String test = "24:00";
System.out.println("TEST: " + new SimpleDateFormat("kk:mm").parse(test));

Output:

TEST: Thu Jan 01 00:00:00 GMT 1970

Expected output:

TEST: Thu Jan 01 24:00:00 GMT 1970

Edit:

System.out.println("TEST: " + new SimpleDateFormat("k:mm:ss").format(new SimpleDateFormat("k:mm:ss").parse(test)));

this prints TEST: Thu Jan 01 24:00:00 GMT 1970

But why not while parsing?

Any help is greatly appreciated

Upvotes: 0

Views: 1167

Answers (1)

jalynn2
jalynn2

Reputation: 6457

The Date.toString() method is defaulting to hh:mm. Use SimpleDateFormat to output it as well as to parse it.

Upvotes: 5

Related Questions