Get Today’s date in Java at midnight time

Get Today’s date in Java at midnight time

Asked on December 28, 2018 in Java.
Add Comment


  • 5 Answer(s)

    Try to do this,

    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.
    

    The output of the above code is Fri Mar 09 00:00:00 IST 2012.

    Answered on December 28, 2018.
    Add Comment

    By using the following code one can get the date list in a range in PostgreSQL,

    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    System.out.println(sdf.format(date));
    
    Answered on December 28, 2018.
    Add Comment

    Have a look at the code,

    Calendar currentDate = Calendar.getInstance(); //Get the current date
    SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss"); //format it as per your requirement
    String dateNow = formatter.format(currentDate.getTime());
    System.out.println("Now the date is :=> " + dateNow);
    
    Answered on December 28, 2018.
    Add Comment
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    System.out.println(sdf.format(date));
    Answered on January 16, 2019.
    Add Comment

    Using org.apache.commons.lang3.time.DateUtils

    Date pDate = new Date();
    DateUtils.truncate(pDate, Calendar.DAY_OF_MONTH);
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.