Want to switch on calendar date doing some calculation - java

I am getting the time in seconds from a function and I have to move further it into days and from the date 2/11/1970 00:00:00 till time in seconds I am getting will be covered. Please help me how to achieve this, or help me to do calculation on dates.

Not sure if I quite get your question, but if I do, have you tried the Calendar object?
Here's an example of how you could get the day out of the time in milliseconds:
Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.setTimeInMillis(date.getTime());
int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);

Take a look at the Joda date time API. IT should cater for your needs. This should point you in the right direction.

Related

How to calculate time difference considering day light saving?

I have function which calculated time difference between two dates in milliseconds. I am getting the time just before 1 hour of day light saving starts and then calculating time after 5 minutes of it. I is giving me 5 minutes of difference, One hour getting skipped. Do anyone having idea?
Just use this:
Suppose date1 is 20.03.2016, 13:00
date2 is 21.03.2016, 17:00
Calendar cal1 = Calendar.getInstance();
cal1.set(Hour_of_day,13);
cal1.set(Calendar.DAY_OF_MONTH, 20);
Calendar cal2 = Calendar.getInstance();
cal2.set(Hour_of_day,17);
cal2.set(Calendar.DAY_OF_MONTH, 21);
then difference of two gives time in milliseconds.
long time_in_milli = cal2.getTimeInMillis()-cal1.getTimeInMillis();
This would already take into account the day light saving thing.
I solved this by including timezone with date ex: Format your date with timezone like - "yyyy-MM-dd'T'HH:mm:ss.SSSZ" and then calculate time difference.

Converting a Joda DateTime to another timezone without changing the time

I have tried all the ways in all the other questions on SO, and I can't get it to work. It is making me want to kill myself.
I have a set of times which are something like "04:00 AM AEST", except the AEST is a glitch, they should be GMT. What I want to do is change them to "04:00 GMT", and then convert them up to the correct AEST times (which in this example would be "14:00 AEST"). I have tried everything, and nothing works. The closest was to manually make a new DateTime using each individual value from the original date, e.g.
DateTime dt = new DateTime(origdate.year, origdate.month, origdate.day, origdate.hour, origdate.minute, origdate.second, timezone.GMT)
But for some reason the results came out four and a half minutes over, which is weird because timezones differ on hours and half hours.
1st Method By following lines you will get GMT time in specified format :
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtTime = date.format(currentLocalTime);
Hence, from GMT you can derive the time of any place.
2nd Method You can get system time of current place in milliseconds by:
Long current_time = System.currentTimeMillis() / 1000L;
Hope it helps.

java Date object- how to increment day of week [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 7 years ago.
I am currently trying to schedule a method for execution once per week on a day which the user will select. I know I can get the current date via:
Date date = new Date();
When setting up my TimerTask for execution, I need to increment the date by 1-6 days depending on which day of week is selected by the user. I do not see a setDay() method in the documentation and was wondering if parsing the day out, changing it, and adding back to the date object is the only way. Seems like something much more simple would be out there.
You need to use a Calendar.
The java.util.Calendar class is an abstract encapsulation of the Date object.
Calendar provides getter and setter for the date fields.
Updated to an example of incrementing the day of the week as requested:
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK,(calendar.get(Calendar.DAY_OF_WEEK)+1));
//alternative:
//calendar.add(Calendar.DAY_OF_WEEK, 1);
Date newDate = calendar.getTime();
Update: Note the java 8+ implementation using java.time
Calendar and Date have not been deprecated, you can still mix and match.
However if you want to handle time zones properly or want to do more localisation (when do you not?) then you are better off using java.time.
public static Date addDays(Date date, int days) {
GregorianCalendar calendar = getCalendar(date);
calendar.add(Calendar.DATE, days);
return calendar.getTime();
}
This should do the trick.
You probably want to use Calendar.
With a Calendar object, you can simply use Calendar.add(Calendar.DAY_OF_WEEK, 1); to add a single day
It would be easy with Calendar class.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1); // this will add number of days to current
// date
Date date = cal.getTime(); // it will return the date object
System.out.println(date);
You may be looking for Date.setDate().
Bear in mind that it's deprecated, and the docs recommend using Calendar.set(Calendar.DAY_OF_MONTH, int date).

Code to find day Difference

I am trying to write function to find day difference between two date it work ok but the result change some time in the same inputs .Let say the current date is 21/7/2014 the result some time is 567 and other time 566.
The code:
//TO GET THE CURRENT DATE
Calendar cal = Calendar.getInstance();
//THE CAL.ADD BECUSE THE 1ST MONTH IN THE YEAR IS 0 NOT 1
cal.add(Calendar.MONTH, 1);
//TO SET THE START DATE WICH IS 1/1/2013
Calendar startDate=Calendar.getInstance();
startDate.set(Calendar.DAY_OF_MONTH, 1);
startDate.set(Calendar.MONTH,1);
startDate.set(Calendar.YEAR, 2013);
//TO FIND THE DIFF BETWEEN THE START DATE AND CUREENT DATE , THE +1 BECUSE IT IS
ALWAYS LESS BY ONE DAY
long diff=(((cal.getTimeInMillis()-
startDate.getTimeInMillis())/(1000*60*60*24))+1);
I think this is because of the hours which get rounded in a strange way..
On my side when I add the following code it seems to work fine:
cal.set(Calendar.HOUR_OF_DAY, 1);
startDate.set(Calendar.HOUR_OF_DAY, 1);
The problem here is when you are instantiating a calendar, you are getting current date and time. So when you ignore time values it works perfectly.
Two compare two dates it's better to use Date class and java and use the following code
So your above code can be rewritten as
Date cal = new Date(114, 7, 21); //Date is 21/7/2014
Date startDate = new Date(113, 1, 1); Date is 1/1/2013
long newdiff = ((cal.getTime()-startDate.getTime())/(1000*60*60*24));
System.out.println(newdiff);
This time it will print 566 correctly (The actuall difference is 566)
The problem really is that suppose you are comparing today's date and tomorrow'date.
ie 21/7/2014 and 22/7/2014. The difference in days is one. If you run the program at 12:00 am midnight you will get 1. and at any other time after that it will result in 0. This is because of getting current time along with the date

Update time only

Is there any way to update only a Date's time path?
I tried Date.setTime() but it replaces the date path too. I there any java method or the only way is to set hour, minute, second and milisecond?
Thank you
A Java Date is just a wrapper around a long that counts time from the epoch (January 1, 1970). Much more flexible is Calendar. You can create a Calendar from a Date:
Date date = . . .;
Calendar cal = new GregorianCalendar();
cal.setTime(date);
Then you can set various fields of the Calendar:
cal.set(Calendar.HOUR_OF_DAY, 8);
// etc.
I would start by moving away from java.util.Date entirely. Ideally, use Joda Time as it's a far more capable date/time library.
Otherwise, you should use java.util.Calendar. A java.util.Date doesn't have a particular date/time until you decide what time zone you're interested in - it just represents an instant in time, which different people around the world will consider to be a different date and time of day.
You'll want to take a look at java.util.Calender.
It will allow you to change the individual parts of the date/time.
Calendar cal = Calender.getInstance();
cal.setTime(date);
cal.set(Calender.HOUR, hour);
Alternatively, as has already being suggested, I'd take a look at Joda Time

Categories

Resources