Budi Darmawan Budi Darmawan - 4 months ago 14
Android Question

get current date and time

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!

Answer

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