问题
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
回答1:
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.
回答2:
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()));
回答3:
Type mismatch: cannot convert from Date to int
change
int day= startDate.getTime();
to
Date dat= startDate.getTime();//return type Date
回答4:
As the javadocs suggest, use Calendar.get(Calendar.DAY_OF_MONTH)
回答5:
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.
回答6:
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)
回答7:
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
回答8:
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);
回答9:
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();
回答10:
Simply type int day = startDate.get(Calendar.DAY_OF_WEEK);
来源:https://stackoverflow.com/questions/32694110/how-to-replace-getdate-from-the-type-date-which-is-deprecated