1

I am collecting data from the call log. This is how I format the date of the call to my taste:

DateFormat datePattern = DateFormat.getDateInstance(DateFormat.FULL);  //Tuesday, October 18, 2011
Long datelong = Long.parseLong("1318950779497");
String date_str = datePattern.format(datelong);
Date date = new Date(date_str);
formatter = new SimpleDateFormat("d/M/yyyy");  //18/10/2011
newdateformatted = formatter.format(date);

This the time of the call:

DateFormat timePattern = DateFormat.getTimeInstance(DateFormat.LONG); //12:40:32 PM GMT+00:00

How can I format this? I want the 24 hour time code.

erdomester
  • 11,789
  • 32
  • 132
  • 234

1 Answers1

2

Just make another formater just for the time:

SimpleDateFormat timeFormater = new SimpleDateFormat("HH:mm:ss");
timeString = timeFormater.format(date);

Note the case-sensitivity of HH:mm:ss. The HH is what gives you the 24 hour time format.

The following little test code works for me:

import java.text.SimpleDateFormat;
import java.util.Date;
public class Test{


  public static void main(String args[]){
    Date today = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    String formated = formatter.format(today);
    System.out.println(formated);
  }
}

It outputs:

15:57:11
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • it's timeFormater.format(date_str), but it gives me an IllegalArgumentException on this line. – erdomester Oct 20 '11 at 20:42
  • Oops, sorry. I edited and fixed it. You have to give it the actual date object, not a string. – Kurtis Nusbaum Oct 20 '11 at 20:45
  • ok. Now it gives me 00:00:00 for all dates. If change the format to kk:mm:ss it gives me 24:00:00. I don't get this. – erdomester Oct 20 '11 at 20:53
  • The `Date` object you're passing it must be set to 00:00:00. Check that your using the correct `Date` object when calling format. I just tried the code and it works fine for me. – Kurtis Nusbaum Oct 20 '11 at 20:58
  • This shows me the current date, because Date today = new Date(); Note that my date is a long at first ("1318950779497"), then I make it a string (Tuesday, October 18, 2011) and I pass it to the Date object: Date date = new Date(date_str);. And this gives me 00:00:00. My guess is that the date_str does not include the time. But I don't how to solve this issue. – erdomester Oct 22 '11 at 12:29
  • How about you just construct the date object using your long, instead of trying to use a string. Like this: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#Date%28long%29 – Kurtis Nusbaum Oct 22 '11 at 17:54
  • This seemed to be a great idea :) Thank you very much for your help! – erdomester Oct 23 '11 at 09:23