Get current date and time

Iโ€™m kind of stuck on a date and time. I want my program to create a date like "20121217". The first four letters are the year, the second is 2 months, and the last 2 is the day. year + month + day

Time "112233" hour + minute + second

Thank you for your help!

+6
source share
7 answers

This is a formatting issue. Java uses java.util.Date and java.text.DateFormat and java.text.SimpleDateFormat for these things.

 DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd hhmmss"); dateFormatter.setLenient(false); Date today = new Date(); String s = dateFormatter.format(today); 
+9
source

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); 
+3
source

Change any specific time or date format as needed.

  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); currentDateandTime = sdf.format(new Date()); 
0
source

Date:

 DateFormat df = new SimpleDateFormat("yyyyMMdd"); String strDate = df.format(new Date()); 

During:

 DateFormat df = new SimpleDateFormat("hhmmss"); String strTime = df.format(new Date()); 
0
source

It works,

  String currentDateTime; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); currentDateTime = sdf1.format(new Date()); 
0
source

What you are looking for is SimpleDateFormat in Java ... Check out this page .

Try this for your needs:

 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hhmmss"); Date parsed = format.parse(new Date()); System.out.println(parsed.toString()); 
-1
source

You can use the following method to get the current time.

  /************************************************************** * getCurrentTime() it will return system time * * @return ****************************************************************/ public static String getCurrentTime() { DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss"); Calendar cal = Calendar.getInstance(); return dateFormat.format(cal.getTime()); }// end of getCurrentTime() 
-1
source

All Articles