0

Is it possible that i can get todays date , rather than time ??

This is my code

public static void main(String args[]) throws ParseException{   
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    Date today = calendar.getTime();

}

Why is todays date is shown as before date ??

public class Ravi {

    public static void main(String args[]) throws ParseException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        System.out.println("Todays Date"+dateFormat.format(date));


        List currentObject = new ArrayList();

        currentObject.add("2012-09-27");

        Date ExpDate = dateFormat.parse((String) currentObject.get(0));

        System.out.println("ddd"+ExpDate);

        if (ExpDate.before(date)) {
            System.out.println("true");
        }

        else {
            System.out.println("false");
        }

    }

}
3
  • What do you want todo with the Date object? Date is a thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.
    – dngfng
    Sep 27, 2012 at 10:36
  • Today's date as a Date or a string? Sep 27, 2012 at 10:36
  • I need to compare it with todays date so that currentdate.before(todaysdate) is false.
    – Pawan
    Sep 27, 2012 at 10:38

6 Answers 6

4
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

It will print 2012-09-27

3

Because Date is always the full current time e.g. 2012.09.27 12:45:23

Whilst your new Formated date is 2012.09.27 00:00:00 therefor the output is correct.

If you want to get false you will need to set hours, minutes and seconds to 0.

Using Calendar:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));

Using Date:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
System.out.println(dateFormat.format(date));

Comparing Dates with Calendar:

Calendar old = Calendar.getInstance();

old.set(Calendar.YEAR, 2011);

Calendar now = Calendar.getInstance();

old.before(now));

Note you may want to set Hours Minutes and Seconds to 0.

1

Getting today date in yyyy-MM-dd format.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String output = sdf.format(new Date());
System.out.println(output);
1

Why is todays date is shown as before date ??

when you do dateFormat.parse("2012-09-27"); date what you will get it will be 00h00min00sec 2012-09-27 so when you compare it with new Date(); you will get today date but couple hours(and minutes, and seconds) later, and that is why "2012-09-27" is before new Date()

0

You can do something like this:

Date date=new Date();
            SimpleDateFormat sd=new SimpleDateFormat("yyyyMMdd HHmmss");
            String strDate=sd.format(date);
            String dateNow=strDate.substring(0,strDate.indexOf(" "));
            String timeNow=strDate.substring(strDate.indexOf(" ")+1);

Hope this helps.

2
  • 1
    Use two distinct SimpleDateFormats instead. One for only date, one for only time.
    – helios
    Sep 27, 2012 at 10:40
  • Correct, I thought probably this is what Preethi Jain was looking for.
    – Atif Imran
    Sep 27, 2012 at 11:10
0
public static void main(String args[]) throws ParseException{   
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    Date today = calendar.getTime();
sysout(dateFormat.format(today))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.