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.
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