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?

0

3 Answers 3

0

That's how I do it:

Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getBaseContext());
dateFormat.format(date);
Sign up to request clarification or add additional context in comments.

Comments

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()); 
    

Comments

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()));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.