Harish Rn
Harish Rn

Reputation: 98

How to access Date and time using Android

I am building an app which starts to run after a user given time... Now can anyone tell me how to access my Mobile's current time so that I can check it with my user given time.

Upvotes: 0

Views: 244

Answers (1)

JakSok
JakSok

Reputation: 124

You can parse whatever you want just change values inside SimpleDateFormat.

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
System.out.println(format.format(new Date()));

If you want just time

SimpleDateFormat format = new SimpleDateFormat("hh:MM:ss");
System.out.println(format.format(new Date()));

Or you can store this datetime in string

DateFormat df = new SimpleDateFormat("HH:mm:ss");

// Get the date
Date today = Calendar.getInstance().getTime();        
// Here you can create a string 
String reportDate = df.format(today);

// For example print date or do what ever you like to do with it
System.out.println("Report Date: " + reportDate);

Upvotes: 1

Related Questions