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");
        }

    }

}
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 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 '12 at 10:36
  • Today's date as a `Date` or a string? – Abhinav Sarkar Sep 27 '12 at 10:36
  • I need to compare it with todays date so that currentdate.before(todaysdate) is false. – Pawan Sep 27 '12 at 10:38

6 Answers6

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

It will print 2012-09-27

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
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.

dngfng
  • 1,923
  • 17
  • 34
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);
swemon
  • 5,840
  • 4
  • 32
  • 54
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()

user902383
  • 8,420
  • 8
  • 43
  • 63
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.

Atif Imran
  • 1,899
  • 2
  • 18
  • 33
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))
}
PermGenError
  • 45,977
  • 8
  • 87
  • 106