I need to get the Current time in a string format in android. I have seen a toString method in Time class. Is there any way to get the current time using Time class or object in android? If not, how can I get the current time as a string in android?
3 Answers
There are multiple possibilities:
Automatically use the user-preferred format:
String dateString = DateFormat.getTimeFormat(thisContext).format(Calendar.getInstance().getTime())Use your own format:
String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
Comments
try to do something like this
Time time = new Time();
System.out.println("time = " + time);
System.out.println("time.toHour() " + time.toHour());
// etc..
// Test with a supplied value
Time time2 = new Time(12033312L);
System.out.println("time2 = " + time2);
also
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));