Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SimpleDateFormat Not Working

Quick question, SimpleDateFormat is not performing as I would expect. I am looking to get a date string that look like Thursday 29 November 13:43.

Here is my format:

Calendar c = Calendar.getInstance();  
_clockDateFormat = new SimpleDateFormat("cccc dd MMMM kk:mm");  
_clockDateFormat.format(c.getTime());

Here is the output:

5 29 11 13:43

What am I doing wrong?

like image 753
FlyingStreudel Avatar asked Nov 30 '25 20:11

FlyingStreudel


2 Answers

Instead of c use E:

Calendar c = Calendar.getInstance();  
SimpleDateFormat _clockDateFormat = new SimpleDateFormat("EEEE dd MMMM kk:mm");  
System.out.println(_clockDateFormat.format(c.getTime())); 

output:

Thursday 29 November 14:05

See the documentation for more info.

like image 182
kosa Avatar answered Dec 02 '25 08:12

kosa


Turns out the device I am working with doesn't have a default locale when it gets here from the factory. As a workaround I used the Locale specific overload for SimpleDateFormat:

_clockDateFormat = new SimpleDateFormat("EEEE dd MMMM HH:mm", Locale.US);

like image 44
FlyingStreudel Avatar answered Dec 02 '25 10:12

FlyingStreudel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!