Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current date and time

Tags:

android

I'm kind of stuck on date and time. I want my program to create the date like this "20121217". The first 4 letters are the year, the second 2 letters are the month and the last 2 are the day. year+month+day

The time is "112233" hour+minute+second

Thanks for your help!

like image 208
Budi Darmawan Avatar asked Dec 17 '12 18:12

Budi Darmawan


People also ask

How do I get the current date and time in python?

To get both current date and time datetime. now() function of DateTime module is used. This function returns the current local date and time.

Does new date () Return current date?

new Date() returns the current time, not the current date.


2 Answers

That's a formatting issue. Java uses java.util.Date and java.text.DateFormat and java.text.SimpleDateFormat for those things.

DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd hhmmss");
dateFormatter.setLenient(false);
Date today = new Date();
String s = dateFormatter.format(today);
like image 133
duffymo Avatar answered Nov 14 '22 23:11

duffymo


You can do something like this:

Calendar c = Calendar.getInstance();
String date = c.get(Calendar.YEAR) + c.get(Calendar.MONTH) + c.get(Calendar.DATE);
String time = c.get(Calendar.HOUR) + c.get(Calendar.MINUTE) + c.get(Calendar.SECOND);
like image 38
Adam Keenan Avatar answered Nov 14 '22 23:11

Adam Keenan