how to detect day light saving in java [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get the current date and time of your timezone in Java?
I have developed a Attendance System and in use for India. Our servers are in US and since they are using PDT. My code reflects time one hour ahead.
say its 9:00 am IST ---- I get the time as 10:00 am IST
other than detecting one hour from the time, which will be a temporary solution.
Pls suggest me some way to overcome this situation

To check if a given Date is affected by daylight saving, use
Calendar c = Calendar.getInstance(timezone); // omit timezone for default tz
c.setTime(date); // your date; omit this line for current date
int offset = c.get(Calendar.DST_OFFSET);
0 means no DST, any other value (most likely 3600000) means that this date is affected by DST

Related

Local timezone date object to UTC timezone date object [duplicate]

This question already has answers here:
Calendar returns date in wrong time zone
(5 answers)
Closed 3 years ago.
I am trying to convert my datetime that is in local timezone into UTC date time.
Date localDate; // this is local date
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ") ;
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateStr = simpleDateFormat.format(localDate);
i am getting proper converted UTC time in dateStr now i want to convert it into Date object with UTC timezone only
but the moment i do that i am again getting the localDate.
//converting string to date object
simpleDateFormat.parse(dateStr)
does anyone know how can i convert local date object to UTC date object
here is the value i am getting while debugging
here dateStr is showing proper date in UTC but utcDate object is showing the local time
Date class is intended to reflect coordinated universal time (UTC) time. It can be formatted into ANY form you want, e.g. you can format it into your local time zone or UTC time zone.
See from javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.

How can I get date, time and day from open weather API in android [duplicate]

This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 4 years ago.
I am trying to get a date, time and day from open weather API of a specific location and specific day using latitude and longitude. But it gives me a long integer something like this 1525974999. How can I retrieve date time and day from this?
Using Java 8 Time API:
Instant.ofEpochSecond(1525974999) // returns: 2018-05-10T17:56:39Z
Using old Java Date:
new Date(1525974999 * 1000L) // returns: Thu May 10 13:56:39 EDT 2018
I'm in Eastern US time zone
The integer represents the amount of time it has been passed since January, 1, 1970. (Unix Time Stamp)
You can use a converter from unix time stamp or just do the math programmatically.
It's probably in seconds. Try this:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeReturnedByAPI * 1000);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
Here is the Calendar API: https://developer.android.com/reference/java/util/Calendar
Edit: You may want to use the version of getInstance that takes a time zone to get the local time.
Edit 2: Updated in response to comments.

Time Zone cannot be changed in Calendar util [duplicate]

This question already has answers here:
Working with various Calendar TimeZone in Java (without using Joda Time)
(3 answers)
Closed 9 years ago.
This sentence is supposed to get time information at Chicago time zone:
Calendar.getInstance(TimeZone.getTimeZone("America/Chicago")).getTime();
My problem is no matter what string I put in getTimeZone(), result would be changed.
Could anyone explain this situation?
The key thing to understand is that a java.util.Date represents UTC only - it has no time zone information. Time zones are presentation layer only - they are used to figure out how to display the time represented by the java.util.Date.
So if you use SimpleDateFormat or the Calendar.get(...) methods, the time zone will be taken into account.

Is java.util.Date represents date as system date or not? [duplicate]

This question already has answers here:
Closed 10 years ago.
I need to get real world date and time in Java. I use:
Date date = new Date();
But I'm not sure that it is not just system time. I don't need to be dependent on PC local date and time.
If it is so, then is there any way to abstract from it? I mean I need correct time and date. If today is the 1st of May, 2012 and user changed (maybe there was a system error) it to the 1st of December 2000, it shouldn't affect business logic. So is there any alternative to achieve this?
Date only represents an instant in time, in milliseconds since the Unix epoch of January 1st 1970 UTC (modulo leap seconds). It has no concept of a time zone in its data. However, if you use the toString method it will always convert that UTC instant to a local date/time using the system time zone. That confuses a lot of users, making them think that Date contains a time zone - it's just an illusion.
Likewise Date doesn't have any concept of a calendar system (Gregorian, Julian etc) or a "format". Basically it's just a long :)

How to convert a data from 1 timezone to another timezone? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Timezone conversion
I have a date in UTC, how to convert it to other timezone?
java.util.Date
Despite what the output of Date.toString() suggests, Date instances are not timezone aware. They simply represent a point in time, irrespective to the timezone. So if you have a Date instance, there is nothing more you need to do. And what if you have time in one time zone and you want to know what is the time in other time zone? You need
java.util.Calendar
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Asia/Tokyo"))
cal.set(Calendar.HOUR_OF_DAY, 15) //15:00 in Tokyo
cal.set(Calendar.MONTH, Calendar.NOVEMBER)
cal.setTimeZone(TimeZone.getTimeZone("Australia/Melbourne"))
cal.get(Calendar.HOUR_OF_DAY) //17:00 in Melbourne
Note that after changing the time zone the date (point in time) didn't changed. Only the representation (current hour in this particular time zone). Also note that November is important there. If we change the month to July suddenly the hour in Melbourne changes to 16:00. That's because Tokyo does not observe DST, while Melbourne does.
java.text.DateFormat
There is another catch in Java with time zones. When you are trying to format a date you need to specify time zone explicitly:
DateFormat format = DateFormat.getTimeInstance
format.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"))
Otherwise DateFormat always uses current computer's time zone which is often inappropriate:
format.format(cal.getTime())
Since format() method does not allow Calendar instances (even though it accepts Object as a parameter - sic!) you have to call Calendar.getTime() - which returns Date. And as being said previously - Date instances are not aware of time zones, hence the Tokyo and Melbourne settings are lost.
You can try Joda-Time library. They have 2 functions called withZone() and withZoneRetainFields() to perform timezone calculations.

Categories

Resources