Round java.util.Date to end of day [duplicate] - java

This question already has answers here:
How to create a Java Date object of midnight today and midnight tomorrow?
(20 answers)
Closed 6 years ago.
I want to round a java.util.Date object to the end of the day, e.g. rounding 2016-04-21T10:28:18.109Z to 2016-04-22T00:00:00.000Z.
I saw Java Date rounding, but wasn't able to find something compareable for the end of the day. It also is not the same as how to create a Java Date object of midnight today and midnight tomorrow?, because I don't want to create a new Date (midnight today or tomorrow), but the next midnight based on any given date.

The DateUtils.ceiling serves your purpose. Pass Calendar.DATE for field value.

Given the documentation of DateUtils, I'm not sure I'd trust it with this.
Assuming you're only interested in a UTC day, you can take advantage of the fact that the Unix epoch is on a date boundary:
public static Date roundUpUtcDate(Date date) {
long millisPerDay = TimeUnit.DAYS.toMillis(1);
long inputMillis = date.getTime();
long daysRoundedUp = (inputMillis + (millisPerDay - 1)) / millisPerDay;
return new Date(daysRoundedUp * millisPerDay);
}
I would strongly urge you to move to the java.time API if you possibly can though.

Traditional way
#Test
public void testDateRound() throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse("2016-04-21T10:28:18.109Z");
System.out.println(date);
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.set(Calendar.HOUR_OF_DAY, 23);
cl.set(Calendar.MINUTE, 59);
cl.set(Calendar.SECOND, 59);
cl.set(Calendar.MILLISECOND, 999);
System.out.println(cl.getTime());
cl.add(Calendar.MILLISECOND, 1);
System.out.println(cl.getTime());
}
Output
Thu Apr 21 10:28:18 GMT+03:00 2016
Thu Apr 21 23:59:59 GMT+03:00 2016
Fri Apr 22 00:00:00 GMT+03:00 2016

Related

DateTimeFormatter and SimpleDateFormat produce different strings [duplicate]

This question already has answers here:
Why when year is less than 1884, it remove few milliseconds?
(2 answers)
Closed 3 years ago.
This is not a duplicate as some people think. It is about two standard Java classes for formatting dates that produce different strings for the same value of milliseconds since the epoch.
For values of milliseconds since the epoch that occur before some point in the year 1883, SimpleDateFormat and DateTimeFormatter will produce different results. For reasons I don't understand, DateTimeFormatter will produce strings that differ from what I expect by almost four minutes.
This is important because I am changing some code to use DateTimeFormatter instead of SimpleDateFormat. Our input is always milliseconds since the epoch, and I need the values to be the same after I change the code.
The previous code would create a Date from the milliseconds, then use SimpleDateFormat to format it.
The new code creates an Instant from the milliseconds, then a ZonedDateTime from the Instant, then a DateTimeFormatter to format it.
Here's a test I wrote using JUnit4 and Hamcrest. The test finds the milliseconds since the epoch for May 13, 15:41:25, for each year starting at 2019 and working backwards one year at a time.
For each year, it formats the milliseconds using SimpleDateFormat and DateTimeFormatter then compares the results.
#Test
public void testSimpleDateFormatVersusDateTimeFormatter() throws Exception {
String formatString = "EEE MMM dd HH:mm:ss zzz yyyy";
String timeZoneCode = "America/New_York";
ZoneId zoneId = ZoneId.of(timeZoneCode);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneCode));
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatString);
for (int year = 0; year < 200; year++) {
long millis = getMillisSinceEpoch(2019 - year, 5, 13, 15, 41, 25, timeZoneCode);
System.out.printf("%s%n", new Date(millis));
// Format using a DateTimeFormatter;
Instant instant = Instant.ofEpochMilli(millis);
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
String dateTimeFormatterString = dateTimeFormatter.format(zonedDateTime);
// Format using a SimpleDateFormat
Date date = new Date(millis);
String simpleDateFormatString = simpleDateFormat.format(date);
System.out.println("dateTimeFormatterString = " + dateTimeFormatterString);
System.out.println("simpleDateFormatString = " + simpleDateFormatString);
System.out.println();
assertThat(simpleDateFormatString, equalTo(dateTimeFormatterString));
}
}
private long getMillisSinceEpoch(int year, int month, int dayOfMonth, int hours, int minutes, int seconds, String timeZoneId) {
TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
Calendar calendar = Calendar.getInstance(timeZone);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
return calendar.getTimeInMillis();
}
Running this you can see it passes for all years from 2019 back to 1884. So for any given year you see output like this:
Mon May 13 12:41:25 PST 1895
dateTimeFormatterString = Mon May 13 15:41:25 EST 1895
simpleDateFormatString = Mon May 13 15:41:25 EST 1895
But once it gets to 1883 it inexplicably fails:
Sun May 13 12:41:25 PST 1883
dateTimeFormatterString = Sun May 13 15:45:23 EST 1883
simpleDateFormatString = Sun May 13 15:41:25 EST 1883
java.lang.AssertionError:
Expected: "Sun May 13 15:45:23 EST 1883"
but: was "Sun May 13 15:41:25 EST 1883"```
The hours and seconds are obviously wrong.
By the way, if I change the time zone to "UTC", then the test passes.
According to https://www.timeanddate.com/time/change/usa/new-york?year=1883 (which was the first hit in a Google search for "1883 time adjustment"):
Nov 18, 1883 - Time Zone Change (LMT → EST)
When local standard time was about to reach
Sunday, November 18, 1883, 12:03:58 pm clocks were turned backward 0:03:58 hours to
Sunday, November 18, 1883, 12:00:00 noon local standard time instead.
3:58 matches the "almost four minutes" that you're seeing.
I haven't tested this, but I bet that if you iterate through months and days in addition to years, it occurs at that date.
See Also
Why when year is less than 1884, it remove few milliseconds?
Python pytz timezone conversion returns values that differ from timezone offset for different dates
Why is subtracting these two times (in 1927) giving a strange result? — a classic answered by Jon Skeet; not the same issue, but the same kind of issue
The Times Reports on "the Day of Two Noons"

How to group list of Dates to corresponding week in Java?

I am working on an Android app that will display a list of activities. Every activity (i.e. waling, running) has a property Date (i.e. 9 March 8:58 2017). I have two buttons on the screen - Daily and Weekly and I want to switch betweren the two and change the list accordingly. Now, for the Daily list, I don't have to change anything, since a new Activity is created for every day.
However, I am not sure how to go about the Weekly list. It will essentially calculate stats (adding up the statistics for the individual weeks).
For example, I have a list of dates for the last 50 days. How to distinguish an individual list of Dates that would represent an individual week so I can construct a list of Weeks? Basically, convert those 50 dates into their week equivalent (e.g. about 7 weeks)
This is a test list of Dates that I am trying to get working first:
HashMap<Integer,Date> dateHashMap = new HashMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Date> dates = new ArrayList<>();
dates.add(sdf.parse("10/03/2017"));
dates.add(sdf.parse("9/03/2017"));
dates.add(sdf.parse("8/03/2017"));
dates.add(sdf.parse("7/03/2017"));
dates.add(sdf.parse("6/03/2017"));
dates.add(sdf.parse("23/02/2017"));
dates.add(sdf.parse("3/02/2017"));
dates.add(sdf.parse("2/02/2017"));
dates.add(sdf.parse("1/02/2017"));
for(Date d:dates){
dateHashMap.put(d.getDay(),d);
}
System.out.println(dateHashMap.toString());
An example UI design that I am trying to achieve:
As you already have Date property for each activity, then its quite simple actually
First just decide how you want your weeks
ex: I would just go from Mon to Sun as one week
So here Week nth will have dates - 1st to 5th March and Week nth+1 will have 6th to 12th March and so on..
and as far as i can understand you already have every activity (i.e. waling, running) with a property Date (i.e. 9 March 8:58 2017)
So taking an example here (let me know if this isn't how you have your data) :
waling - 1 March 2017 8:58 to 9:58, 3 March 2017 6:20 to 6:50, 8 March 2017 12:00 to 13:00
running - 2 March 2017 6:10 to 8:00, 3 2017 March 7:00 to 8:00, 9 March 2017 5:50 to 7:00
Now data for Week nth you can calculate by adding up duration for waling activity for dates 1st and 3rd March as waling was present only on these dates on Week nth of March 2017 and similarly for week nth+1 and so on
Same goes for running activity for week nth adding up for dates 2nd March, 3rd March and similarly for week nth+1 and so on..
Now you will have something like :
Week nth :
Wailing - 1 hr and 30 min
Running - 2 hrs and 50 min
Week nth+1 :
Wailing - 1 hr
Running - 1 hr and 10 min
And on clicking of each activity you can show some more details..
Hope this helps :)
Edit :
Considering this is how you have your dates list
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Date> dates = new ArrayList<>();
try {
dates.add(sdf.parse("10/03/2017"));
dates.add(sdf.parse("9/03/2017"));
dates.add(sdf.parse("8/03/2017"));
dates.add(sdf.parse("7/03/2017"));
dates.add(sdf.parse("6/03/2017"));
dates.add(sdf.parse("23/02/2017"));
dates.add(sdf.parse("3/02/2017"));
dates.add(sdf.parse("2/02/2017"));
dates.add(sdf.parse("1/02/2017"));
} catch (ParseException e) {
e.printStackTrace();
}
You can create a custom List just to identify the dates that falls in the same week ex (I just used what #Jameson suggested in his answer, you can always write this a lot better):
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
And you can use getListOfWeeksFromListOfDates(dates); to have a list with Dates and Week number, this week number can serve as an identifier to compare the dates and then you can add the activities for dates with same Week number.. Hope you are getting what i am trying to convey here :)
/**
* Gets a list of week numbers (as strings) from a list of dates.
*
* #param listOfDates the list of dates
* #return a list of week in year (as string), corresponding
* one-to-one to the values in the input list.
*/
public List<String> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<String> listOfWeeks = new ArrayList<>();
for (Date date : listOfDates) {
listOfWeeks.add(new SimpleDateFormat("w").format(date));
}
return listOfWeeks;
}
Week?
You have not defined what you mean by week.
Do you mean the standard ISO 8601 week where each week starts on a Monday and week # 1 contains the first Thursday, each week numbered 1-52 or 53?
Or do you mean a United States type week beginning on a Sunday with weeks numbered 1-52/53 starting with January 1, and if so are the last days of the previous week in the previous year or the new year?
Or do you mean something else?
Time zone?
What time zone do want to use as the context for determining the date? Or do you want to keep your date-times in UTC like Stack Overflow does in tracking your activity for “today” vs “yesterday”?
Avoid legacy date-time classes
The troublesome old date classes including Date and Calendar should be avoided whenever possible. They are now supplanted by the java.time classes.
Convert your given Date objects to Instant, a moment on the timeline in UTC.
Instant instant = myDate.toInstant();
ISO 8601
I suggest using the standard week whenever possible.
Adjust your Instant into the desired time zone.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
Retrieve the standard week number.
int weekNumber = zdt.get( WeekFields.ISO.weekOfWeekBasedYear() );
Keep in mind that the year number to go with this week number is not the calendar year. We want the year of the week-based year. For example, in some years, December 30 and 31 can belong to the following year number of a week-based year.
int yearOfWeekBasedYear = zdt.get( WeekFields.ISO.weekBasedYear() );
You could track your records against a string composed of this yearOfWeekBasedYear and weekNumber. Use standard format, yyyy-Www such as 2017-W07.
ThreeTen-Extra YearWeek
Instead I suggest you use meaningful objects rather than mere strings. Add the ThreeTen-Extra library to your project to gain the YearWeek class.
This code replaces the WeekFields code we did above.
YearWeek yw = YearWeek.from( zdt );
Thanks to #shadygoneinsane and # Basil Bourque for pointing me to the right direction I solved the problem the following way:
TreeMap<Integer, List<Date>> dateHashMap = new TreeMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<Date> dates = new ArrayList<>();
dates.add(sdf.parse("10/03/2017"));
dates.add(sdf.parse("9/03/2017"));
dates.add(sdf.parse("8/03/2017"));
dates.add(sdf.parse("7/03/2017"));
dates.add(sdf.parse("6/03/2017"));
dates.add(sdf.parse("23/02/2017"));
dates.add(sdf.parse("3/02/2017"));
dates.add(sdf.parse("2/02/2017"));
dates.add(sdf.parse("1/02/2017"));
for (int i = 0; i < dates.size(); i++) {
List<Date> datesList = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dates.get(i));
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
for (Date date : dates) {
Calendar c = Calendar.getInstance();
c.setTime(date);
if (weekOfMonth == c.get(Calendar.WEEK_OF_MONTH)) {
datesList.add(date);
}
}
dateHashMap.put(weekOfMonth, datesList);
}
System.out.println(dateHashMap.toString());
}
And the result:
Output:
1=[Fri Feb 03 00:00:00 GMT 2017,
Thu Feb 02 00:00:00 GMT 2017,
Wed Feb 01 00:00:00 GMT 2017],
2=[Fri Mar 10 00:00:00 GMT 2017,
Thu Mar 09 00:00:00 GMT 2017,
Wed Mar 08 00:00:00 GMT 2017,
Tue Mar 07 00:00:00 GMT 2017,
Mon Mar 06 00:00:00 GMT 2017],
4=[Thu Feb 23 00:00:00 GMT 2017]
Exactly what I needed! So now I can iterate through each week and sum up the statistics and thus formulate the "Weekly" view of the list

Why string '35/35/1985` parses to `Sat Dec 05 00:00:00 IST 1987` date object? [duplicate]

This question already has answers here:
What is the use of "lenient "?
(7 answers)
Closed 7 years ago.
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String dateStr = "35/35/1985";
Date date = sdf.parse(dateStr);
i was expecting run time exception like date parse exception but returned date object is Sat Dec 05 00:00:00 IST 1987
By what logic string 35/35/1985 parsed to date Sat Dec 05 00:00:00 IST 1987?
update:- If I set the setLenient(false), it throws exception. But if I make it true
By what logic string 35/35/1985 parsed to date Sat Dec 05 00:00:00 IST 1987?
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String dateStr = "35/35/1985";
Date date = sdf.parse(dateStr);
To answer your "logic behind" question: Well, it will be parsed as
xx.xx.1985
-> set / add 35 Months (xx.35.1982 -> xx.11.1987)
-> set / add 35 Days (35.11.1987 -> 05.12.1987)
If you don't want this behaviuor, set lenient to false:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient%28boolean%29
To keep track of time, Java counts the number of milliseconds from the start of January 1, 1970. This means, for example, that January 2, 1970, began 86,400,000 milliseconds later. (...) The Java Date class keeps track of those milliseconds as a long value. Because long is a signed number, dates can be expressed before and after the start of January 1, 1970.
From JavaWorld.com
Basically, Java's engine does not know what a Date is, so a date is nothing but a reference to how many milliseconds have passed since the "Beginning" and it will then be converted to a nice MM/DD/YYYY format. Same thing in the other direction. So technically, 35/35/1985 is not a mistake, it simply means "substract 34 months, 34 days and 1985 years to 0 months, 0 days and 1970 years".
This can be useful if you are calculating Loans for example where people tend to reference 5 years as "60 months". See the point?
In you above code
you mentioned
year => 1985
month=> 35
day =>35
now first, year is 1985. if month is 12 then it will be 1985 after that when month is 24 it will be 1986. above 24, year will be 1987 and for month 35 it is Nov 1987. now your date is 35 which is above 30 so it will go to December 5 with adding one year i.e 1987.
so finally Dec 5 1987.

problems with java.util.Calendar and timeZones

I have following method which convert my custom DMY (date,month,year) object to Date.
public static Date serverCreateDateFromDMY(DMY pDMY, TimeZone pTimeZone)
{
Calendar vCalendar = Calendar.getInstance(pTimeZone);
vCalendar.set(Calendar.YEAR, pDMY.getYear());
// Below line is because DMY month counts are 1-indexed
// and Date month counts are 0-indexed
vCalendar.set(Calendar.MONTH, pDMY.getMonthOfYear() - 1);
vCalendar.set(Calendar.DAY_OF_MONTH, pDMY.getDayOfMonth());
System.out.println(vCalendar.getTime());
TimeUtilsServer.zeroCalendarHoursAndBelow(vCalendar);
System.out.println(vCalendar.getTime());
return vCalendar.getTime();
}
public static void zeroCalendarHoursAndBelow(Calendar pToZero)
{
pToZero.set(Calendar.HOUR_OF_DAY, 0);
pToZero.set(Calendar.MINUTE, 0);
pToZero.set(Calendar.SECOND, 0);
pToZero.set(Calendar.MILLISECOND, 0);
}
to serverCreateDateFromDMY() method, I am passing these arguments : DMY=20120424, and TimeZone is : America/New_York. Application is running locally in my timezone which is IST.
based in above inputs, following output is printed.
Tue Apr 24 14:43:07 IST 2012
Tue Apr 24 09:30:00 IST 2012
so as you see that in last output time is not zeroed out. any suggestions please?
#Marko, yes I come to know about DateFormat and I tried following example. but still date is printed with time and not zeroing out.
TimeZone tz = TimeZone.getTimeZone("America/New_York");
Calendar vCalendar = Calendar.getInstance(tz);
vCalendar.set(Calendar.YEAR, 2012);
vCalendar.set(Calendar.MONTH, 4 - 1);
vCalendar.set(Calendar.DAY_OF_MONTH, 24);
DateFormat df = DateFormat.getDateTimeInstance();
df.setTimeZone(tz);
System.out.println(df.format(vCalendar.getTime()));
vCalendar.set(Calendar.HOUR_OF_DAY, vCalendar.getActualMinimum(Calendar.HOUR_OF_DAY));
vCalendar.set(Calendar.MINUTE, vCalendar.getActualMinimum(Calendar.MINUTE));
vCalendar.set(Calendar.SECOND, vCalendar.getActualMinimum(Calendar.SECOND));
vCalendar.set(Calendar.MILLISECOND, vCalendar.getActualMinimum(Calendar.MILLISECOND));
System.out.println(df.format(vCalendar.getTime()));
java Date / Time API have a bad design from the time of its creation. Maybe you should take a look at some library - for example this which hides JDK API deficiencies - http://joda-time.sourceforge.net/
Internally, Date and Calendar objects are stored in UTC. When you set the fields to 0, the Calendar is updated in UTC.
When you ask the Calendar for the time, it then converts the Date to your desired Timezone, hence the difference.
... and you are 9:30h ahead of NY time. You set the time to midnight NY time and read it out as time in your zone. Note that getTime returns a Date, which is not timezone-configurable. You'll need DateFormat if you want to specify the timezone for which you print the result.

How to compare dates using Java [duplicate]

This question already has answers here:
How to compare dates in Java? [duplicate]
(11 answers)
Closed 6 years ago.
I have a date with the format 2012-02-02(yyyy-MM-dd).
For example if todays date is 2012-02-02 i need to add one and a half days to it which would make it 2012-02-03 06:00:00.0.
And if i have a number of dates of the following format 2012-02-03 06:30:00.0(yyyy-MM-dd HH:MM:SS.SSS) , i need to compare if all these dates are less than,greater than or equal to the date to which one and a half days were added above.
The comparison should also take care of the hours while comparing if the dates are less than,greater than or equal or equal to the other date and time.
How do i achieve the same.
Simple arithmetic approach (faster)
Parse the date using SimpleDateFormat that creates a Date object
Use Date.getTime() to return the UTC value in long
Convert 1 and half days to millis (1.5 Days = 129600000 Milliseconds) and add it to previous step
Use >, < and == or after(), before() and equals() if you want to use Date object itself
API approach (slower)
Use Calendar
add(...) method for adding 1 and half day
use before(), after() and equals() methods of Calendar
well so i hope this will give you a clear idea. Calendar Documentation and SimpleDateFormat Documentaion
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String aDateString = "2012-02-02";
Date date = sdf.parse(aDateString);
System.out.println("reference date:"+date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, 36);
System.out.println("added one and half days to reference date: "+cal.getTime());
String newDateString = "2012-02-03 06:30:00.0";
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date newDate = sdf.parse(newDateString);
System.out.println("new date to compare with reference date : "+newDate);
Calendar newCal = Calendar.getInstance();
newCal.setTime(newDate);
if(cal.after(newCal)){
System.out.println("date is greater than reference that.");
}else if(cal.before(newCal)){
System.out.println("date is lesser than reference that.");
}else{
System.out.println("date is equal to reference that.");
}
OUTPUT :
reference date:Thu Feb 02 00:00:00 IST 2012
added one and half days to reference date: Fri Feb 03 12:00:00 IST 2012
new date to compare with reference date : Fri Feb 03 06:30:00 IST 2012
date is greater than reference that.
Use SimpleDateFormat to convert String to Date
Set date to Calendar instance
use calendar.add(Calendar.HOUR, 36)
Also See
Joda Time API
You need to use Joda date time API.
String strDate="2012-02-02";
DateTime dateTime=DateTime.parse(strDate);
DateTime newDateTime=dateTime.plusHours(18);
System.out.println(dateTime);
System.out.println(newDateTime);

Categories

Resources