Converting date and time to string, wrong clock

I do this in my android application (java):

String sdt_ = DateFormat.format("yyyyMMdd HH:mm", dt_).toString(); 

but i got it

 01-16 14:31:13.308: D/ThS(25810): dt_ = Wed Jan 16 13:28:00 GMT+00:00 2013 01-16 14:31:23.758: D/ThS(25810): sdt_ = 20130116 HH:28 

if i change hh to hh i get this

 sdt_ = 20130116 01:28 

but i need it

 sdt_ = 20130116 13:28 
+4
source share
3 answers

I don't know what is wrong with your code, but this code works for me:

 DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm"); String sdt = df.format(new Date(System.currentTimeMillis())); System.out.println(sdt); 

EDIT:

I later found out that your source code should work with this:

 String sdt_ = DateFormat.format("yyyyMMdd kk:mm", dt_).toString(); 

obviously android.text.format.DateFormat does not use the "H" constraint and uses the "k" instead! see this question for more details: How to set 24 hour date format in java?

+7
source

This will do it for you:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm", Locale.getDefault()); String sdt_ = sdf.format(dt_); 
+4
source

You used hh in your SimpleDateFormat template. This is a 12 hour format. Instead, use kk, which gives you the hours of the day in 24 hour format. Take a look at SimpleDateFormat

+3
source

All Articles