3

I have an existing program that I have to correct . It contains these lines :

        Date startDate = new Date();
        int day = startDate.getDate() - 1;

but getDate() from the type Date is deprecated so i have to change it using Calender. I tried this :

Calendar startDate = Calendar.getInstance();
startDate.add(Calendar.DATE, -1);
int day= startDate.getTime();

but this results into following error :

Type mismatch: cannot convert from Date to int

Yuri
  • 447
  • 3
  • 9
  • 19

10 Answers10

6

One more good option is to use like :

System.out.println(DateFormat.getDateInstance().format(new Date()));

It will print the current date.

If you need time along with date then you can use like :

System.out.println(DateFormat.getDateTimeInstance().format(new Date()));
FullStack
  • 665
  • 11
  • 26
5

If you want to get day in month use this:

int day= startDate.get(Calendar.DAY_OF_MONTH);

If you want to get day in week use this:

int day= startDate.get(Calendar.DAY_OF_WEEK);

Also be careful about day of week, because day 0 is sunday not monday.

Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

Jure
  • 799
  • 6
  • 25
  • seems like Calender class is not supported from GWT. I got this error while compiling : " No source code is available for type java.util.Calendar; did you forget to inherit a required module?" – Yuri Sep 21 '15 at 12:37
  • I didn't notice that you use GWT. I am not familiar with GWT but check this response: http://stackoverflow.com/questions/7009655/how-to-use-java-util-calendar-in-gwt – Jure Sep 21 '15 at 12:51
0

Type mismatch: cannot convert from Date to int

change

  int day= startDate.getTime();

to

 Date dat= startDate.getTime();//return type Date
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
0

As the javadocs suggest, use Calendar.get(Calendar.DAY_OF_MONTH)

ennovation
  • 366
  • 2
  • 9
0

To get the day of the month:

int day= startDate.get(Calendar.DAY_OF_MONTH);

From the Javadocs:

Field number for get and set indicating the day of the month. This is a synonym for DATE. The first day of the month has value 1.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

The getTime() function will return a Date Object which cannot be converted to int. If you want to get the day as an integer, you have to use:

int day = startDate.get(Calendar.DATE)
Daniel Pflüger
  • 479
  • 7
  • 10
0

See on java doc at http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#getTime():

Date object of getTime() method return long not in int so use like:

long time = startDate.getTime();

For Calendar docs http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTime() Use like below:

long time = startDate.getTime().getTime();

For day of month:

Calendar c = Calendar.getInstance();
int dayOfMonth = c.get(Calendar.DATE);
Bhuwan Prasad Upadhyay
  • 2,916
  • 1
  • 29
  • 33
0

In Date#startDate.getDate() returns the day of the month :

  • Code#1-->

    Date startDate = new Date();
    int day = startDate.getDate() - 1;
    System.out.println(day); 
    

Through Calendar#.get(Calendar.DAY_OF_MONTH) you will get the same result as Date#startDate.getDate():

  • Code#2-->

    Calendar startDate = Calendar.getInstance();
    int day= startDate.get(Calendar.DAY_OF_MONTH)-1;
    System.out.println(day);
    

So you can replace Code#1 by Code#2

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
0

java.time

The old java.util.Date/.Calendar classes are notoriously troublesome. They have been supplanted in Java 8 and later by the new java.time framework.

Note the use of time zone. Crucial in determining a date. If omitted, you implicitly rely on the JVM’s current default time zone. Better to specify the desired/expected time zone.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( zoneId );
int dayOfMonth = now.getDayOfMonth();
int dayOfWeek = now.getDayOfWeek().getValue();
int dayOfYear = now.getDayOfYear();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Simply type int day = startDate.get(Calendar.DAY_OF_WEEK);