0

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?

manlio
  • 18,345
  • 14
  • 76
  • 126
irfan
  • 869
  • 3
  • 12
  • 25

3 Answers3

0

That's how I do it:

Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getBaseContext());
dateFormat.format(date);
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
0

There are multiple possibilities:

  1. Automatically use the user-preferred format:

    String dateString = DateFormat.getTimeFormat(thisContext).format(Calendar.getInstance().getTime())
    
  2. Use your own format:

    String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()); 
    
FD_
  • 12,947
  • 4
  • 35
  • 62
0

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()));
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43