0

I want to retain CST time always with offset -6, at present I am getting as 2018-03-15T05:08:53-05:00.

But I want to change it as with offset -6 like 2018-03-15T05:08:53-06:00 through out the year.

TimeZone tz= TimeZone.getdefault();

if(tz.inDayLightTime())
{
    getCSTDate(cal)
    // I would like to change the logic here. 
}

public XMLGregorianCalendar getCSTDate(Calendar cal)
{
    return XMLGregorianCalendar; 
}

my input type : calendar output : XMLGregorianCalendar

jcrshankar
  • 1,165
  • 8
  • 25
  • 45
  • 3
    Can you explain *why* you want to keep -6 the entire year? Do you have a scenario or a physical place that does this? If so, please elaborate - there is likely a better solution. – Matt Johnson-Pint Mar 16 '18 at 18:22
  • Does your code compile? – Ole V.V. Mar 16 '18 at 18:24
  • The `TimeZone` and `Calendar` classes are long outdated. Likely there is also a better solution than using `XMLGregorianCalendar`, so you may also want to elaborate on why you want one. – Ole V.V. Mar 16 '18 at 18:26
  • 1
    @VikramPalakurthi The *Joda-Time* project is now in maintenance-mode, with its team advising migration to the *java.time* classes. – Basil Bourque Mar 16 '18 at 18:46
  • Your comments are entirely different than your question here. Please take more care to think through your issues before posting. And search before posting – all these issues have been covered on Stack Overflow. – Basil Bourque Mar 16 '18 at 20:36

2 Answers2

2

Then don't use a timezone that tracks Daylight Saving Time changes (which is probably the case of yours TimeZone.getDefault()).

If you want a fixed offset, you can do:

TimeZone tz = TimeZone.getTimeZone("GMT-06:00");

Not sure why you want that, because if you're dealing with timezones, you must consider DST effects. And 2018-03-15T05:08:53-06:00 is not the same instant as 2018-03-15T05:08:53-05:00, so changing the offset while keeping all the other fields is usually wrong - as it's not clear why you want that and what you want to achieve, I can't give you more advice on that.

watssu
  • 57
  • 4
  • am checking the day belongs to same day or previous day, am expecting the day should be same day. for some reason on 11/march mid night.. the date 2018-03-11T04:08:53-05:00 , early morning 4am.. but when i compare calendar date with 2018-03-11T04:08:53-05:00 , it gave me miss match. – jcrshankar Mar 16 '18 at 14:27
  • 3
    @jcrshankar Perhaps you should edit your question then, to explain all of this and people can come up with a proper answer. Because checking if the day is the same has nothing to do with CSTxCDT offsets (at least based on the information on the question) – watssu Mar 16 '18 at 14:28
  • FYI, [`TimeZone`](https://docs.oracle.com/javase/9/docs/api/java/util/TimeZone.html) is part of the troublesome old date-time classes that are now legacy, and should be avoided. Supplanted by the *java.time* classes, specifically [`ZoneId`](https://docs.oracle.com/javase/9/docs/api/java/time/ZoneId.html) and [`ZoneOffset`](https://docs.oracle.com/javase/9/docs/api/java/time/ZoneOffset.html). – Basil Bourque Mar 16 '18 at 20:38
1

tl;dr

If you want the current moment as seen through a fixed offset-from-UTC, use OffsetDateTime with ZoneOffset.

OffsetDateTime.now( 
    ZoneOffset.ofHours( -6 )
)

Details

always with offset -6

The Answer by watssu is correct: If you don’t want the effects of Daylight Saving Time (DST), don’t use a time zone that respects DST.

If you always want an offset-from-UTC fixed at six hours behind UTC, use an OffsetDateTime.

ZoneOffset offset = ZoneOffset.ofHours( -6 ) ;
OffsetDateTime odt = OffsetDateTime.now( offset ) ;  // Ignores DST, offset is fixed and unchanging. 

Be clear that an offset is simply a number hours, minutes, and seconds displacement from UTC. In contrast, a time zone is a history of past, present, and future changes in offset used by the people of a particular region. So generally, you should be using a time zone rather than a mere offset. Your insistence on a fixed offset is likely unwise.

The 3-4 letter abbreviations such as CST are not time zones. They are used by mainstream media to give a rough idea about time zone and indicate if DST is in effect. But they are notstandardized. They are not even unique! For example, CST means Central Standard Time as well as China Standard Time or Cuba Standard Time.

Use real time zones with names in the format of continent/region.

Avoid all the legacy date-time classes such as TimeZone now supplanted by the java.time classes. Specifically, ZoneId.

ZoneId z = ZoneId.of( "America/Chicago" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;  // Respects DST changes in offset. 

If your real issue is wanting to detect DST to alter your logic, I suggest you rethink the problem. I suspect you are attacking the wrong issue. But if you insist, you can ask for the offset currently in effect on your ZonedDateTime, and you can ask a ZoneId if DST is in effect for any particular moment via the ZoneRules class.

ZoneOffset offsetInEffect = zdt.getOffset() ;

And…

Boolean isDstInEffect = zdt.getZone.getRules().isDaylightSavings( zdt.toInstant() ) ;

On that last line, note the incorrect use of plural with s on isDaylightSavings.

The XMLGregorianCalendar class is part of the troublesome old legacy date-time classes, now supplanted by the java.time classes, specifically ZonedDateTime. To inter-operate with old code not yet updated to java.time, convert to the modern class via the legacy class GregorianCalendar.

ZonedDateTime zdt = myXmlCal.toGregorianCalendar().toZonedDateTime() ;

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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154