I am seeing this issue while using Calendar instance to subtract one hour from time when day light saving ends and we transition to standard time.
here is the code:
Date startTime = new Date();// Gives me Sun Nov 06 01:26:16 EST 2016
Calendar temp;
TimeZone timezone = TimeZone.getDefault(); //Eastern
temp= Calendar.getInstance(timezone);
temp.setTime(startTime);
temp.add(Calendar.HOUR_OF_DAY, -1);
temp.set(Calendar.MILLISECOND, 0);
Date endDate = temp.getTime(); // This is still Sun Nov 06 01:26:16 EST 2016
The result that I expect in endDate is Sun Nov 06 01:26:16 EDT 2016 instead of Sun Nov 06 01:26:16 EST 2016. I am not sure if this is as designed or not. If I subtract 2 hours, then I see it working fine.
Any inputs on this?
Thanks,
SS
Using java.time
You should be using java.time classes rather than the troublesome old date-time classes such as Calendar than are now legacy.
Be sure to read the class doc for ZonedDateTime to understand its choice of how to handle the Daylight Saving Time (DST) cut-overs.
Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or EDT or IST as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/New_York" );
// of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)
ZonedDateTime zdt = ZonedDateTime.of( 2016 , 11 , 6 , 1 , 26 , 16 , 0 , z );
ZonedDateTime zdtOneHourEarlier = zdt.minusHours( 1 );
ZonedDateTime zdtOneHourLater = zdt.plusHours( 1 );
Note the offset-from-UTC in each result.
zdt.toString(): 2016-11-06T01:26:16-04:00[America/New_York]
zdtOneHourEarlier.toString(): 2016-11-06T00:26:16-04:00[America/New_York]
zdtOneHourLater.toString(): 2016-11-06T01:26:16-05:00[America/New_York]
See this code run live at IdeOne.com.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Java SE 8 and SE 9 and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
Related
A couple of years ago I wrote a proof of concept function to date strings to calculate the end date from the string. This never made it to production for a number of reasons but all the functions worked. For example the string
999 years less 1 day from 16 August 2000
should evaluate to 15th August 2999. I wrote this function using the Microsoft Recognizers library (V1.3.2) and it all worked.
var culture = Culture.MapToNearestLanguage("en-gb");
DateTimeOptions dateTimeOptions;
dateTimeOptions = DateTimeOptions.ExtendedTypes;
var results = DateTimeRecognizer.RecognizeDateTime(leaseTermPhrase, culture, dateTimeOptions);
results would return 3 elements derived from the string, a duration of 999 years, a duration of 1 day and a date of 16 August 2000, and using that data I was able to calculate the end date (I made an assumption that any second duration should be taken away from the end date).
I am now revisiting this code as the need for it has come back into scope. I updated the Recognizer packages to the latest version (1.8.2) and now the unit test that tests this particular format of string fails (other string formats still pass).
Upon investigation I find that the results variable now only contains two parts; a duration of 999 years and a Date 0f 15 August 2000. So the new library is parsing '1 day from 16 August 2000' as a single date entity instead of two separate ones (a duration and a date)
Does anyone know if it is possible to get the Recognizer to produce the same results as previously?
I want to create a categorical variable for the semester column in my dataset. I have other additional variables with the target-not shown in the table.
Training set: include 2016-2017
Test set or validation set: include only 2018
My Concern is when I make the predictive model I will have categorical variables (factors) that do not exist in the training set (i.e SPRING 2018, SUMMER 2018–First SESSION,...etc). Is this will be a problem theoretically? How to deal with that?
Start End Semester
Jan 19,2016 May 6,2016 SPRING 2016
May 16,2016 Jun 25,2016 SUMMER 2016-FIRST SESSION
Jun 27,2016 Aug 6,2016 SUMMER 2016-SECOND SESSION
Aug 24,2016 Dec 16,2016 FALL 2016
Jan 17,2017 May 5,2017 SPRING 2017
May 15,2017 Jun 24,2017 SUMMER 2017–First SESSION
Jun 26,2017 Aug 5,2017 SUMMER 2017-SECOND SESSION
Aug 23,2017 Dec 15,2017 FALL 2017
Jan 16,2018 May 4,2018 SPRING 2018
May 14,2018 June 23,2018 SUMMER 2018–First SESSION
Jun 25,2018 Aug 4,2018 SUMMER 2018-SECOND SESSION
Aug 22,2018 Dec 14,2018 Fall 2018
The machine learning algorithms learn patterns in data, if we do not have any repeated pattern then with high probability they failed to provide adequate answer. I think you need to transform adequate information to your model for getting a rational output. Regarding to your research question, it can be different:
For instance, If you want to answer the question of when is the starting and ending time of semester x in year y?
You can convert the semester column into 4 ordinal categorical variables of 1 to 4 for Spring to Fall. In addition you should provide a year column in your data and DD,MM for ending and starting time.
One part of my assignment requires me to use gregorian calendar in the student class.
Every student has a unique studentNumber (int), name (String), **
dateRegistered (GregorianCalendar)
** , id (String) and courseEnrolled (CourseOffering).
import java.util.Calendar;
import java.util.GregorianCalendar;
public abstract class Student
{
private int studentNumber;
private String name;
private String id;
private CourseOffering courseEnrolled;
private GregorianCalendar startDate = new GregorianCalendar();
public Student( int studentNumber, String name, String id, CourseOffering courseEnrolled){
this.studentNumber = studentNumber;
this.name = name;
this.id = id;
this.courseEnrolled = courseEnrolled;
}
public int getStudentNumber(){
return studentNumber;
}
public String toString(){
return String.format("%08d", studentNumber) + " " + name + " " + id;
}
}
I wasn't taught on how to use the calendar in school yet, but this came up for the assignment.
"The constructor that does all the necessary initializations given all necessary values including the day (int), month (int) and year (int) of the registration date "
How do i put the day month year in the constructor ? Help guys :(
tl;dr
LocalDate.of( 2018 , 1 , 23 ) // January 23, 2018.
Use java.time classes instead. Unlike the legacy classes, these modern classes use sane numbering:
2018 is year 2018.
1-12 for January-December.
1-7 for Monday-Sunday.
The java.time classes use static factory methods rather than new constructor calls.
ZonedDateTime
The GregorianCalendar class is now obsolete (good riddance). Replaced by ZonedDateTime.
ZonedDateTime zdt =
ZonedDateTime.of(
LocalDate.of( 2018 , Month.JANUARY , 23 ) ,
LocalTime.of( 18 , 30 ) ,
ZoneId.of( "Africa/Tunis" )
)
;
zdt.toString(): 2018-01-23T18:30+01:00[Africa/Tunis]
LocalDate
The LocalDate class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. 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(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
Or, better, use the Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety.
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
I am working in MVC4 project where i am facing problem with time.Actually my project requirement is to show all time used in application according to Brazil time.So i have used GETUTCDATE() for saving time in application.
Now i want to check if DST is on according to time i saved..i mean central time. How do i check this.
I have search on net and found one solution
DECLARE #IsDST BIT;
SET #IsDST = CASE WHEN DateDiff(hour, GetDate(), GetUTCDate()) = 4 THEN 'True'
ELSE 'False' END;
SELECT GETDATE() AS GETDATE,
GETUTCDATE() AS GETUTCDATE,
#IsDST;
But when i try to run this script,it return false ??
But as per DST calculation,it always starts from 2nd Sunday of March and ends on 1st Sunday of November.
Then it should return true ,that DST is on.
Am i doing right or is there another better approach to check if DST is on central time,so that i can show brazil time according to DST
Well, this particular code doesn't work for detecting DST in Brazil, because it just measures the difference right now between local time and UTC, checking for 4 hours difference or not.
Most of Brazil is 3 hours behind UTC in the standard time, and 2 hours behind UTC in the daylight time. So this code probably won't work for you. You can read more in this Wikipedia article.
Daylight Saving Time is very different all over the world, so if you intend to use this approach then you will have to modify your code to match the time zone of the server that it's running on.
Personally, I would recommend not doing this in SQL at all. Time zone conversions aren't really the realm of the database. They work much better in application code. You should work with UTC in your database, and convert it to Brazil or whatever time zone you require in your application.
Since you said this was an ASP.Net MVC4 application, I recommend you either use the .net TimeZoneInfo class, or use the excellent Noda Time library to do your conversions in your .Net code.
Using TimeZoneInfo:
DateTime utcDT = // ... your UTC value returned from the database
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(
"E. South America Standard Time"); // Brazil
DateTime brazilDT = TimeZoneInfo.ConvertTimeFromUtc(utcDT, tz);
Using Noda Time:
DateTime utcDT = // ... your UTC value returned from the database
Instant instant = Instant.FromDateTimeUtc(utcDT);
DateTimeZone tz = DateTimeZoneProviders.Tzdb["America/Sao_Paulo"]; // Brazil
ZonedDateTime brazilZDT = instant.InZone(tz);
DateTime brazilDT = brazilZDT.ToDateTimeUnspecified();
I am having problems with my code and not sure what is wrong with it.
NSString* dateFormat1 = #"EEE MMM dd HH:mm:ss V yyyy";
NSString* dateString1 = #"Fri Jul 26 00:00:00 PHT 2013";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:dateFormat1];
My result for iOS5.1:
2013-08-02 11:59:08.459 MySandbox[41502:c07] 2013-07-25 16:00:00 +0000
For iOS 6.0
2013-08-02 12:02:37.454 MySandbox[41581:c07] (null)
*Note, I used V, instead of zzz or Z because it still is null.
*Note 2, I tried to change PHT to PST and it worked. Changed it to JST, it didn't work.
I am guessing that I am not using the correct format. Not really sure which I should use.
*Note 3, as per this question, there really was a change in iOS5 and iOS6 so I guess that is the reason why iOS5 worked.
According to these docs from Apple, NSDateFormatter uses the Unicode date format patterns, which are part of "Unicode Technical Standard #35". This standard is evolving, and iOS 5 uses version 19 while iOS 6 uses version 25.
You can read about the supported values for the time zone in "Appendix J: Time Zone Display Names" (version 19 or version 25).
I am not exactly sure why PHT was supported before and not it isn't. But in general, the best advice I can give is to avoid using time zone abbreviations. While PHT is unique, there are many other abbreviations that are ambiguous. See the list here.
Instead, you should uses the IANA time zone name whenever possible. (The tr-35 standard calls this the "Golden Zone"). In your case, it would be Asia/Manila. You can find a full list here. I am not an Objective C developer, so I'm not sure exactly how you would use this in your input string. You can try the VVVV format specifier, but I'm not sure if it directly accepts the IANA name or not.
You might want to check the results from [NSTimeZone abbreviationDictionary] as shown in this answer. In that post, it does show PHT = "Asia/Manila", but I assume by the date that it was posted that these were generated from iOS 5.