Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Today's date in Java at midnight time

Tags:

java

date

I need to create two date objects. If the current date and time is March 9th 2012 11:30 AM then

  • date object d1 should be 9th March 2012 12:00 AM
  • date object d2 should contain the current date itself

The date will not be entered, it is system date.

Update:

Date dt = new Date(); System.out.print(dt.toString()); 

gives current date and time

like image 411
Akhil K Nambiar Avatar asked Mar 09 '12 06:03

Akhil K Nambiar


People also ask

How do you call today's date in Java?

Code To Get Today's date in any specific FormatgetTime(); String todaysdate = dateFormat. format(date); System. out. println("Today's date : " + todaysdate);

Is Java date in UTC?

java. util. Date has no specific time zone, although its value is most commonly thought of in relation to UTC.


2 Answers

Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); System.out.println(sdf.format(date)); 
like image 43
yen1k Avatar answered Sep 23 '22 05:09

yen1k


    Calendar c = new GregorianCalendar();     c.set(Calendar.HOUR_OF_DAY, 0); //anything 0 - 23     c.set(Calendar.MINUTE, 0);     c.set(Calendar.SECOND, 0);     Date d1 = c.getTime(); //the midnight, that's the first second of the day. 

should be Fri Mar 09 00:00:00 IST 2012

like image 121
Nishant Avatar answered Sep 23 '22 05:09

Nishant