9

i have made an application in which i need to perform date conversion in 24 hour format. Here is my code.

GregorianCalendar c = new GregorianCalendar(Locale.GERMANY);
            c.set(2011, 04, 29,0,0,0);
            String cdate = (String) DateFormat.format("yyyy-MM-dd HH:mm:ss", c.getTime());
            Log.i(tag,cdate);

now when i check my LOG here is the output:

04-22 12:44:15.956: INFO/GridCellAdapter(30248): 2011-04-29 HH:00:00

why is the hour field not getting set. i have explicitly passed 0 when i was making the calendar object, still it is display HH in the LOG. i have tried while passing hh, that is being set, but HH is not being set.

what could be the problem?

thank you in advance.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
user590849
  • 11,655
  • 27
  • 84
  • 125

3 Answers3

15
String cdate = (String) DateFormat.format("yyyy-MM-dd kk:mm:ss", c.getTime());
animuson
  • 53,861
  • 28
  • 137
  • 147
kp666
  • 480
  • 3
  • 7
  • 2
    HH should be 0-23 according to the documentation, but for me returns HH in the string, if you use kk it gives you 0-23 not 1-24 like it says. http://developer.android.com/reference/java/text/SimpleDateFormat.html – draksia Feb 27 '13 at 19:02
  • 1
    HOUR_OF_DAY for Androids DateFormat helper class should return 0 at midnight as stated in the documentation. http://developer.android.com/reference/android/text/format/DateFormat.html#HOUR_OF_DAY – Daniel H. Jul 17 '13 at 07:49
10

Try using SimpleDateFormat, a subclass of DateFormat. That might correct the problems you're having.

e.g. to print current datetime

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_H-mm-ss",Locale.US);
String formattedDateString = dateFormat.format(new java.util.Date());
// result is something like "2013-02-14_18-44-15"
tmanthey
  • 4,547
  • 6
  • 35
  • 42
Rajath
  • 11,787
  • 7
  • 48
  • 62
  • Suggest you give a link back from your other question (http://stackoverflow.com/questions/5753170/how-to-pass-hour-minute-and-second-in-calendar-object-in-android-java) to here – Rajath Apr 22 '11 at 12:13
2

Try this

Long someLongDate = 240000L 
// I was working with a Long in my own code. 
// I'm sure you can use something like varDate.getLong()
TextView textView = (TextView) view;                 // your view of interest
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");     
textView.setText(sdf.format(new Date(someLongDate)));

If it helped - don't forget to check my post as the answer..

Norfeldt
  • 8,272
  • 23
  • 96
  • 152