Java Date determine if DAY is greater
I have time stamps as string format Sun Jul 10 17:47:55 EDT 2011
I need to determine if the current DAY is greater than the stored day.
I will get the current day with
Date currentDate = new Date();
and I will parse my string into a Date
object with SimpleDateFormat dateParser = new SimpleDateParser("E MMM d HH:mm:ss z y");
but using the "currentDate.开发者_如何学Cafter()" function, or the currentDate.compare()
function will only tell me if ANY date is greater or less than, which includes by the hour,minute or second.
So My next hunch would be to convert the date into the day of the year, and compare the new integers, if integer1>integer2, then.. but how would I do that?
I also considered breaking the string up to a substring consisting of only the first half of the stored string date. Sun Jul 10
to Sun Jul 10
but the problem here is that the day value is sometimes 1 digit and othertimes 2 digits.
Also I think the Calendar
abstract class is the best way to go about this, but I am unsure, and currently in a fog about how to convert the Date objects into the Calendar object for comparison!
Insight appreciated
This may be helpful in your case (as quick way):
Locale.setDefault(Locale.UK);
String storedDateString = "Sun Jul 10 17:47:55 EDT 2011";
SimpleDateFormat dateParser = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
Date storedDate = dateParser.parse(storedDateString);
GregorianCalendar storedCal = new GregorianCalendar();
storedCal.setTime(storedDate);
GregorianCalendar currentCal = new GregorianCalendar();
int storedDayOfYear = storedCal.get(Calendar.DAY_OF_YEAR);
int currentDayofYear = currentCal.get(Calendar.DAY_OF_YEAR);
//int storedDayYear = storedCal.get(Calendar.YEAR);
//int currentDayYear = storedCal.get(Calendar.YEAR);
System.out.println(storedDayOfYear);
System.out.println(currentDayofYear);
//System.out.println(storedDayYear);
//System.out.println(currentDayYear);
Result:
191
192
After that it's trivial to compare int values.
If you want to convert a Date object into a Calendar objet, use
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
You can get the day value of the Calendar object with
cal.get(Calendar.DAY_OF_MONTH);
or
calendar.get(Calendar.DAY_OF_YEAR);
however, note that when comparing dates of different months (first example) or years (second example) this wont work. You should better set the milliseconds, seconds, minutes and hours to 0 on each Calendar object and the compare the objects:
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
c1.set(Calendar.MILLISECOND, 0);
c1.set(Calendar.SECOND, 0);
c1.set(Calendar.MINUTE, 0);
c1.set(Calendar.HOUR, 0);
c2.set(Calendar.MILLISECOND, 0);
c2.set(Calendar.SECOND, 0);
c2.set(Calendar.MINUTE, 0);
c2.set(Calendar.HOUR, 0);
c1.compareTo(c2);
Calendar cal = Calendar.getInstance(); // use the getInstance(TimeZone zone) overload
// if you want some other timezone than the
// default one
cal.setTime(yourDate);
cal.get(Calendar.DAY_OF_MONTH);
First of all, convert the Date object to String using toString()
. Once this is done, split the String using split(" ")
. This will return an String[]
. Since both the formats are same, hence in both the arrays, the third element will contain the date
and second element will be month
.
If the month is same, then you only have to see which date is bigger than the other (You can also check the years).
tl;dr
ZonedDateTime.parse(
"Sun Jul 10 17:47:55 EDT 2011" ,
DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US )
).getDayOfYear()
191
Day-of-year
Apparently you want to compare the day-of-year of two dates, how far into the year each date goes regardless of the year and regardless of the time-of-day.
This day-of-year idea is often mislabeled as Julian dates, but is properly known as an Ordinal Date. And even that includes a year, such as 2017-186
. But apparently you want simply the day with in the year, a number from 1 to 365 or 366.
Using java.time
The modern approach uses the java.time classes that supplant the troublesome old date-time classes you are using.
First parse your string as a ZonedDateTime
.
String input = "Sun Jul 10 17:47:55 EDT 2011" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US ) ;
ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;
zdt.toString(): 2011-07-10T17:47:55-04:00[America/New_York]
By the way, never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!). Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
.
Lastly, get the day-of-year integer number you seek.
int dayOfYear = zdt.getDayOfYear() ;
dayOfYear: 191
See this code run live at IdeOne.com.
精彩评论