Date difference calculation error - java

The following code does not appear to calculate the correct value for spanNow. Having just run the code today spanYear is correct at 31535999 but for spanNow I have a value 23363788 which appears wrong. The string representation s correctly contains Tue Oct 29 09:56:28 GMT 2013. However 23363788 / 3600 / 24 to get day of year gives 270. The 270's day of the year is Sept 27th, not 29th October. Why the discrepancy and what is the correct way to accurately calculate the number of seconds between two dates?
Thanks Mark
Calendar c = Calendar.getInstance();
int year=c.get(Calendar.YEAR);
GregorianCalendar g = new GregorianCalendar(year,1,1,0,0,0);
Date start = g.getTime();
g = new GregorianCalendar(year,12,31,23,59,59);
Date end = g.getTime();
long spanYear = TimeUnit.MILLISECONDS.toSeconds(end.getTime()-start.getTime());
Date now = new Date();
String s = now.toString();
long spanNow = TimeUnit.MILLISECONDS.toSeconds(now.getTime()-start.getTime());

From the documentation:
month - the value used to set the MONTH calendar field in the calendar. Month value is 0-based. e.g., 0 for January.
so use
GregorianCalendar g = new GregorianCalendar(year,0,1,0,0,0);

Related

Convert date Format yy/totaldaysinyear/totalsecondsinday to dd/mm/yyyy hh/mm/ss

I have this format YY/Number of date : 14 316 57627
14 : is the year 2014
316 : is the number of the day within the year
57627 : is the number of seconds passed since the beginning of the day
I need to recover to dd/mm/yyyy hh/mm/ss date format
The question is if there is some Java method or library that can calculate that.
There are two elements to this question. First you need to calculate the appropriate Date value from your input format, which you should be able to do using java.util.Calendar:
Calendar cal = new GregorianCalendar();
cal.clear();
cal.set(Calendar.YEAR, year + 2000);
cal.set(Calendar.DAY_OF_YEAR, dayNumber);
cal.add(Calendar.SECOND, numSeconds); // add handles overflow from one field to next
Date date = cal.getTime();
Once you have the right date you can format it however you like using a SimpleDateFormat.

To find wednesday between to dates given by the user

I need to find wednesday for the two dates given by the user.
example:
Inputs are:
from date:07-Feb-2013
To date:13-feb-2013
The gap between the from date and To date is 7 days always.
Expected Output:12-feb-2013
public String getAutoDayExpiryDateAndToDate(String instrmentId,String deliveryAutoFromDate)
throws SystemException, FunctionalException,ParseException
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(deliveryAutoFromDate));
Date fromDate=calendar.getTime();
SimpleDateFormat sf1 = new SimpleDateFormat("dd-MMM-yyyy");
String formatedDate = sf1.format(fromDate);
calendar.add(Calendar.WEEK_OF_MONTH, 1);
calendar.add(Calendar.DAY_OF_WEEK,-1);
Date time = calendar.getTime();
SimpleDateFormat sf = new SimpleDateFormat("dd-MMM-yyyy");
String formatedDate1 = sf.format(time);
}
after these i need to find Wednesday which is present between formatedDate and formatedDate1 .how can i do it??
Use the Calendar class. Set it to the first date, and then check if the current day of the week is Wednesday by calling calendar.get(Calendar.DAY_OF_WEEK). Perform this check in a loop, adding a day to the current date during each iteration. This will never take more than seven steps, so you don't need to do anything fancier than that.
this should find the first Wednesday after (or equal) the given date
GregorianCalendar c = new GregorianCalendar(2013, 1, 7);
if (c.get(Calendar.DAY_OF_WEEK) <= Calendar.WEDNESDAY) {
c.add(Calendar.DAY_OF_YEAR, Calendar.WEDNESDAY - c.get(Calendar.DAY_OF_WEEK));
} else {
c.add(Calendar.DAY_OF_YEAR, 11 - c.get(Calendar.DAY_OF_WEEK));
}
System.out.println(c.getTime());
prints
Wed Feb 13 00:00:00 EET 2013
you can test it to see if it gives you what you want

Java subtract two dates in this format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculating difference in dates in Java
How do you subtract Dates in Java?
I am parsing two dates from a string that look like:
Oct 15, 2012 1:07:13 PM
Oct 23, 2012 03:43:34 PM
What I need to do is find the difference between these two dates, ex:
Oct 23, 2012 03:43:34 PM - Oct 15, 2012 1:07:13 PM
= 8 days 2 hours 36 minutes 21 seconds
^ This is what I need to get with the two date/times I have
I believe I need to parse the format and convert it to another format, then subtract the difference between and do the math to get the days/hours/minutes/seconds between
In contrary to what other answerers try to imply, calculating the difference between two dates isn't that trivial in standard Java SE.
Your first step is indeed to convert those strings to useable Date instances. You can do this using SimpleDateFormat. Here's a kickoff example:
String string1 = "Oct 15, 2012 1:07:13 PM";
String string2 = "Oct 23, 2012 03:43:34 PM";
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy h:mm:ss a", Locale.ENGLISH);
Date date1 = sdf.parse(string1);
Date date2 = sdf.parse(string2);
(please note the importance of the optional Locale argument here, this is often overlooked in answers about converting strings to dates)
Your next step is calculating the difference between those 2 dates. This is a terrible job when you are restricted to the standard Java SE API. Best what you can get is the java.util.Calendar.
Note that you could of course substract the milliseconds and calculate the difference using the usual arithmetic operators.
long differenceInMillis = date2.getTime() - date1.getTime();
// ...
But this naive approach doesn't take leap years into account, let alone daylight saving time and local-specific changes in datetime.
As to the java.util.Calendar approach, you basically need to use Calendar#add() in a counter loop to get the elapsed value for years, months and days. This takes leap years, daylight saving time and local-specific disturbances in time properly into account.
First create this helper method to eliminate some boilerplate code:
public static int elapsed(Calendar before, Calendar after, int field) {
Calendar clone = (Calendar) before.clone(); // Otherwise changes are been reflected.
int elapsed = -1;
while (!clone.after(after)) {
clone.add(field, 1);
elapsed++;
}
return elapsed;
}
Now you can calculate the elapsed time as follows:
Calendar start = Calendar.getInstance();
start.setTime(date1);
Calendar end = Calendar.getInstance();
end.setTime(date2);
Integer[] elapsed = new Integer[6];
Calendar clone = (Calendar) start.clone(); // Otherwise changes are been reflected.
elapsed[0] = elapsed(clone, end, Calendar.YEAR);
clone.add(Calendar.YEAR, elapsed[0]);
elapsed[1] = elapsed(clone, end, Calendar.MONTH);
clone.add(Calendar.MONTH, elapsed[1]);
elapsed[2] = elapsed(clone, end, Calendar.DATE);
clone.add(Calendar.DATE, elapsed[2]);
elapsed[3] = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 3600000;
clone.add(Calendar.HOUR, elapsed[3]);
elapsed[4] = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 60000;
clone.add(Calendar.MINUTE, elapsed[4]);
elapsed[5] = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 1000;
System.out.format("%d years, %d months, %d days, %d hours, %d minutes, %d seconds", elapsed);
Pretty ugly, yeah.
If you going to work with date and time in Java pretty often, then you may find Joda time the walhalla. Here's a concrete kickoff example of how you could do it all with pure Joda Time:
String string1 = "Oct 15, 2012 1:07:13 PM";
String string2 = "Oct 23, 2012 03:43:34 PM";
DateTimeFormatter dtf = DateTimeFormat.forPattern("MMM d, yyyy h:mm:ss a").withLocale(Locale.ENGLISH);
DateTime dateTime1 = dtf.parseDateTime(string1);
DateTime dateTime2 = dtf.parseDateTime(string2);
Period period = new Period(dateTime1, dateTime2);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" years ")
.appendMonths().appendSuffix(" months ")
.appendWeeks().appendSuffix(" weeks ")
.appendDays().appendSuffix(" days ")
.appendHours().appendSuffix(" hours ")
.appendMinutes().appendSuffix(" minutes ")
.appendSeconds().appendSuffix(" seconds ")
.printZeroNever()
.toFormatter();
String elapsed = formatter.print(period);
System.out.println(elapsed);
Much better, right? The plural "s" needs some work though, but that's beyond the question.
You need to use SimpleDateFormat to parse String and create Date
Then you can find the difference between dates.
Here is javadoc for SimpleDateFormat
try this:
Calendar ca1 = Calendar.getInstance();
ca1.set(2012,05,25);
// Addition of date in java
ca1.add(Calendar.DATE, 23); // Add 23 days in Dates in Calendar
ca1.add(Calendar.MONTH, 2); // Add 2 Month in Date in Calendar
ca1.add(Calendar.YEAR, 4); // add 4 Year in Date in Calendar
ca1.add(Calendar.DATE, -23); // sub 23 days in Dates in Calendar
ca1.add(Calendar.MONTH, -2); // sub 2 Month in Date in Calendar
ca1.add(Calendar.YEAR, -4); // sub 4 Year in Date in Calendar

Why does GregorianCalendar.getInstance contain a calsys and cdate of type Julian Calendar

I tried to do set date value to a PreparedStatement with default value but the value is sometimes returned as a JulianValue. For example (Assume spanBegin and spanEnd are null)
Calendar cal = new GregorianCalendar();
if (spanBegin == null) {
cal.set(0000, Calendar.JANUARY, 1);
spanBegin = cal.getTime();
}
if (spanEnd == null)
{
cal.set(9999, Calendar.DECEMBER, 31);
spanEnd = cal.getTime();
}
On line number 3, since the date January 1, 0000 is scoped by a Julian Calendar, the CDate becomes a Julian Calendar. However, the next Date even if it is in the year 9999, its CDate becomes a Julian Calendar still. I had to create another instance of Gregorian Calendar to fix the issue.
Calendar cal = new GregorianCalendar();
if (spanBegin == null) {
cal.set(0000, Calendar.JANUARY, 1);
spanBegin = cal.getTime();
}
Calendar cal = new GregorianCalendar();
if (spanEnd == null)
{
cal.set(9999, Calendar.DECEMBER, 31);
spanEnd = cal.getTime();
}
The question is, is the this an expected behavior or a bug on the date object? Actually using GregorianCalendar.getInstance() shows that the cdate is sometimes set to JulianCalendar.
There was no Gregorian Calendar until 1582. The Julian calendar was in use all over Europe, until minor problems started to appear caused by the fact the solar year is not exactly 365.25 days, but a little less than that. In order to fix things, pope Gregory XIII ordered to change the calendar to what we know today - every year that divides by 100 is not a leap year, unless it divides by 400. In October 1582 there was the transition - the day after 4 Oct was 15 Oct. This means that until October 1582, the Gregorian and Julian Calendars are the same. You can read more about it here
This is why dates prior to Oct 1582 are converted to use the Julian system. According to the API If you actually need to represent an historical event (which seems not to by the case here) you can do it only from 1st march, 4AD
What version of Java are you using and on what OS? Do you really need to store dates in the years 0 and 9999, or are you just using these as "negative infinity" and "positive infinity" values? How exactly do you see that the calendar is a Julian calendar?
I tried this:
Calendar cal = Calendar.getInstance();
cal.set(0, Calendar.JANUARY, 1);
Date d1 = cal.getTime();
cal.set(9999, Calendar.DECEMBER, 31);
Date d2 = cal.getTime();
System.out.println(d1);
System.out.println(d2);
Output (on Windows XP, using Sun Java 1.6.0_16):
Thu Jan 01 09:53:56 CET 1 java.util.Date
Tue Dec 31 09:53:56 CET 9999 java.util.Date
It changes the year 0 to the year 1. Changing the code to use a second Calendar object for the second date:
Calendar cal = Calendar.getInstance();
cal.set(0, Calendar.JANUARY, 1);
Date d1 = cal.getTime();
Calendar cal2 = Calendar.getInstance();
cal2.set(9999, Calendar.DECEMBER, 31);
Date d2 = cal2.getTime();
System.out.println(d1);
System.out.println(d2);
This does not change anything to the output or the content of the two Date objects.
Note: Beware that integer literals that start with a 0, such as 0000 in your code, will be interpreted as octal numbers by the Java compiler. That doesn't matter in this case because the number is 0, but you should not prepend integer literals with zeroes if you don't mean them as octal numbers.
Thhere is no year 0 in Julian calendar. It goes from 1 BC to 1 AD.

Get Previous Day [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How to determine the date one day prior to a given date in Java?
If I have a Java.Util.Date object, what is the best way to get an object representing the 24 hours in the past of it?
Using Java 1.6 java.util.Calendar.add:
public static Date subtractDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
Others suggest using Joda Time, which is currently JSR 310, and should later be included in Java itself.
The important thing to remember is that the Date class should represent any points in time whilst the Calendar class is used to manipulate those points in time. Last of all, SimpleDateFormat will represent them as Strings.
So, the best way is to use the Calendar class to calculate the new Date for you. This will ensure that any vagaries (Daylight Saving, Leap Years and the like) are accounted for.
I'm assuming that you don't really want to find '24 Hours previous' but actually do want a new Date instance representing 'this time yesterday' - either way, you can ask the Calendar instance for a Date 24Hours prior to another or 1 Day prior.
The Daylight savings is a great example. The UK 'sprang forward' on the 26th March 2009. So, 1 day prior to 3.00a.m. on the 26.Mar.2009 should yield 3.00a.m. 25.Mar.2009 but 24 Hrs prior will yield 2.00a.m.
public class DateTests extends TestCase {
private static String EXPECTED_SUMMER_TIME = "2009.Mar.29 03:00:00";
private static String EXPECTED_SUMMER_TIME_LESS_DAY = "2009.Mar.28 03:00:00";
private static String EXPECTED_SUMMER_TIME_LESS_24_HRS = "2009.Mar.28 02:00:00";
private static String EXPECTED_SUMMER_TIME_LESS_FURTHER_24_HRS = "2009.Mar.27 02:00:00";
public void testSubtractDayOr24Hours() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MMM.dd HH:mm:SS");
Calendar calendar = Calendar.getInstance();
// Create our reference date, 3.00 a.m. on the day the clocks go forward (they 'went' forward at 02.00)
calendar.clear();
calendar.set(2009, 2, 29, 3, 0);
Date summerTime = calendar.getTime(); // Sun Mar 29 03:00:00 BST 2009
String formattedSummerTime = formatter.format(summerTime);
calendar.add(Calendar.DAY_OF_MONTH, -1);
// Our reference date less 'a day'
Date summerTimeLessADay = calendar.getTime(); // Sat Mar 28 03:00:00 GMT 2009
String formattedSummerTimeLessADay = formatter.format(summerTimeLessADay);
// reset the calendar instance to the reference day
calendar.setTime(summerTime);
// Our reference date less '24 hours' (is not quite 24 hours)
calendar.add(Calendar.HOUR, -24);
Date summerTimeLess24Hrs = calendar.getTime(); // Sat Mar 28 02:00:00 GMT 2009
String formattedSummerTimeLess24Hrs = formatter.format(summerTimeLess24Hrs);
// Third date shows that taking a further 24 hours from yields expected result
calendar.add(Calendar.HOUR, -24);
Date summerTimeLessFurther24Hrs = calendar.getTime(); // Fri Mar 27 02:00:00 GMT 2009
String formattedSummerTimeLessFurther24Hrs = formatter.format(summerTimeLessFurther24Hrs);
// reset the calendar once more to the day before
calendar.setTime(summerTimeLess24Hrs);
// Take a 'day' from the Sat will yield the same result as date 03 because Daylight Saving is not a factor
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date summerTimeLessFurtherDay = calendar.getTime(); // Fri Mar 27 02:00:00 GMT 2009
String formattedSummerTimeLessFurtherDay = formatter.format(summerTimeLessFurtherDay);
assert(formattedSummerTime.equals(EXPECTED_SUMMER_TIME));
assert(formattedSummerTimeLessADay.equals(EXPECTED_SUMMER_TIME_LESS_DAY));
assert(formattedSummerTimeLess24Hrs.equals(EXPECTED_SUMMER_TIME_LESS_24_HRS));
assert(formattedSummerTimeLessFurther24Hrs.equals(EXPECTED_SUMMER_TIME_LESS_FURTHER_24_HRS));
// This last test proves that taking 24 hors vs. A Day usually yields the same result
assert(formattedSummerTimeLessFurther24Hrs.equals(formattedSummerTimeLessFurtherDay));
}
}
For testing date functions, wwwdot-timeanddate-dot-com is a great resource.
subtract 1000*60*60*24 from the time and create a new date.
Date yesterday = new Date(d.getTime() - (1000*60*60*24));
int dayInMs = 1000 * 60 * 60 * 24;
Date previousDay = new Date(olddate.getTime() - dayInMs);
Personally if there are a lot of time/date calculations, I'd go with Joda-time.

Categories