user2385057
user2385057

Reputation: 537

Java Data Format Error

Any Clues on how to fix this:

SimpleDateFormat df = new SimpleDateFormat("yyyy/mm/dd hh:24mi:ss");

Exception:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'i'
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:696)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:515)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:464)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:445)
at CopyEJ.CopyEJ.main(CopyEJ.java:105)

Upvotes: 0

Views: 920

Answers (4)

AlexWien
AlexWien

Reputation: 28747

Your date format pattern string contains the invalid sequence 24mi.

change to:

SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

note: the Big letter "HH" means 24 hours: e.g 23:59:00
while "hh" is 12 hours format: 11:59:00

Upvotes: 4

mtk
mtk

Reputation: 13717

As pointed by others, the pattern string is not correct, and can be as showed by others "yyyy/MM/dd HH:mm:ss".

Refer the java documentation for looking up the available pattern letters that could be used for formatting a date.

Snippet from the above link

Letter  Date or Time Component  Presentation    Examples
G   Era designator  Text    AD
y   Year    Year    1996; 96
M   Month in year   Month   July; Jul; 07
w   Week in year    Number  27
W   Week in month   Number  2
D   Day in year     Number  189
d   Day in month    Number  10
F   Day of week in month    Number  2
E   Day in week     Text    Tuesday; Tue
a   Am/pm marker    Text    PM
H   Hour in day (0-23)  Number  0
k   Hour in day (1-24)  Number  24
K   Hour in am/pm (0-11)    Number  0
h   Hour in am/pm (1-12)    Number  12
m   Minute in hour  Number  30
s   Second in minute    Number  55
S   Millisecond     Number  978
z   Time zone   General time zone   Pacific Standard Time; PST; GMT-08:00
Z   Time zone   RFC 822 time zone   -0800

Upvotes: 2

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35577

If you want hours in 24 hour format Use this

    DateFormat df=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date dd=new Date();
    System.out.println(df.format(dd));

Upvotes: 1

Jan D&#246;rrenhaus
Jan D&#246;rrenhaus

Reputation: 6717

Your pattern string is not following the java standard. This should work:

SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

Upvotes: 7

Related Questions