Search in sources :

Example 26 with SimpleTimeZone

use of java.util.SimpleTimeZone in project joda-time by JodaOrg.

the class TestLocalDate_Basics method testToDate_autumnDST.

public void testToDate_autumnDST() {
    LocalDate base = new LocalDate(2007, 10, 2);
    SimpleTimeZone testZone = new SimpleTimeZone(3600000, "NoMidnight", Calendar.APRIL, 2, 0, 0, Calendar.OCTOBER, 2, 0, 3600000);
    TimeZone currentZone = TimeZone.getDefault();
    try {
        TimeZone.setDefault(testZone);
        Date test = base.toDate();
        check(base, 2007, 10, 2);
        assertEquals("Tue Oct 02 00:00:00 GMT+02:00 2007", test.toString());
    } finally {
        TimeZone.setDefault(currentZone);
    }
}
Also used : TimeZone(java.util.TimeZone) SimpleTimeZone(java.util.SimpleTimeZone) SimpleTimeZone(java.util.SimpleTimeZone) Date(java.util.Date)

Example 27 with SimpleTimeZone

use of java.util.SimpleTimeZone in project pdfbox by apache.

the class DateConverter method formatTZoffset.

/*
     * Formats a time zone offset as #hh^mm
     * where # is + or -, hh is hours, ^ is a separator, and mm is minutes.
     * Any separator may be specified by the second argument;
     * the usual values are ":" (ISO 8601), "" (RFC 822), and "'" (PDF).
     * The returned value is constrained to the range -11:59 ... 11:59.
     * For offset of 0 millis, the String returned is "+00^00", never "Z".
     * To get a "general" offset in form GMT#hh:mm, write
     *      "GMT"+DateConverter.formatTZoffset(offset, ":");
     *
     * Take thought in choosing the source for the millis value. 
     * It can come from calendarValue.getTimeZone() or from 
     * calendarValue.get(Calendar.ZONE_OFFSET).  If a TimeZone was created
     * from a valid time zone ID, then it may have a daylight savings rule.
     * (As of July 4, 2013, the data base at http://www.iana.org/time-zones 
     * recognized 629 time zone regions. But a TimeZone created as 
     *      new SimpleTimeZone(millisOffset, "ID"), 
     * will not have a daylight savings rule. (Not even if there is a
     * known time zone with the given ID. To get the TimeZone named "xDT"
     * with its DST rule, use an ID of EST5EDT, CST6CDT, MST7MDT, or PST8PDT.
     *
     * When parsing PDF dates, the incoming values DOES NOT have a TIMEZONE value.
     * At most it has an OFFSET value like -04'00'. It is generally impossible to 
     * determine what TIMEZONE corresponds to a given OFFSET. If the date is
     * in the summer when daylight savings is in effect, an offset of -0400
     * might correspond to any one of the 38 regions (of 53) with standard time 
     * offset -0400 and no daylight saving. Or it might correspond to 
     * any one of the 31 regions (out of 43) that observe daylight savings 
     * and have standard time offset of -0500.
     *
     * If a Calendar has not been assigned a TimeZone with setTimeZone(), 
     * it will have by default the local TIMEZONE, not just the OFFSET.  In the
     * USA, this TimeZone will have a daylight savings rule.
     *
     * The offset assigned with calVal.set(Calendar.ZONE_OFFSET) differs
     * from the offset in the TimeZone set by Calendar.setTimeZone(). Example:
     * Suppose my local TimeZone is America/New_York. It has an offset of -05'00'.
     * And suppose I set a GregorianCalendar's ZONE_OFFSET to -07'00'
     *     calVal = new GregorianCalendar();   // TimeZone is the local default
     *     calVal.set(Calendar.ZONE_OFFSET, -7* MILLIS_PER_HOUR);
     * Four different offsets can be computed from calVal:
     *     calVal.get(Calendar.ZONE_OFFSET)  =>  -07:00
     *     calVal.get(Calendar.ZONE_OFFSET) + calVal.get(Calendar.DST_OFFSET) => -06:00
     *     calVal.getTimeZone().getRawOffset()  =>  -05:00
     *     calVal.getTimeZone().getOffset(calVal.getTimeInMillis())  =>  -04:00
     *
     * Which is correct??? I dunno, though setTimeZone() does seem to affect
     * ZONE_OFFSET, and not vice versa.  One cannot even test whether TimeZone 
     * or ZONE_OFFSET has been set; both have been set by initialization code.
     * TimeZone is initialized to the local default time zone 
     * and ZONE_OFFSET is set from it.
     * 
     * My choice in this DateConverter class has been to set the 
     * initial TimeZone of a GregorianCalendar to GMT. Thereafter
     * the TimeZone is modified with {@link #adjustTimeZoneNicely}.
     *
     * package-private for testing
     */
static String formatTZoffset(long millis, String sep) {
    // #hhmm
    SimpleDateFormat sdf = new SimpleDateFormat("Z");
    sdf.setTimeZone(new SimpleTimeZone(restrainTZoffset(millis), "unknown"));
    String tz = sdf.format(new Date());
    return tz.substring(0, 3) + sep + tz.substring(3);
}
Also used : SimpleTimeZone(java.util.SimpleTimeZone) COSString(org.apache.pdfbox.cos.COSString) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 28 with SimpleTimeZone

use of java.util.SimpleTimeZone in project JFramework by gugumall.

the class HmacSHA1 method getISO8601Time.

/**
 * get timestamp
 * @return
 */
public static String getISO8601Time() {
    Date nowDate = new Date();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    df.setTimeZone(new SimpleTimeZone(0, "GMT"));
    return df.format(nowDate);
}
Also used : SimpleTimeZone(java.util.SimpleTimeZone) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 29 with SimpleTimeZone

use of java.util.SimpleTimeZone in project apache-kafka-on-k8s by banzaicloud.

the class SegmentsTest method shouldUpdateSegmentFileNameFromOldDateFormatToNewFormat.

@Test
public void shouldUpdateSegmentFileNameFromOldDateFormatToNewFormat() throws Exception {
    final String storeDirectoryPath = stateDirectory.getAbsolutePath() + File.separator + storeName;
    final File storeDirectory = new File(storeDirectoryPath);
    storeDirectory.mkdirs();
    final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
    formatter.setTimeZone(new SimpleTimeZone(0, "UTC"));
    for (int segmentId = 0; segmentId < NUM_SEGMENTS; ++segmentId) {
        final File oldSegment = new File(storeDirectoryPath + File.separator + storeName + "-" + formatter.format(new Date(segmentId * segmentInterval)));
        oldSegment.createNewFile();
    }
    segments.openExisting(context);
    for (int segmentId = 0; segmentId < NUM_SEGMENTS; ++segmentId) {
        final File newSegment = new File(storeDirectoryPath + File.separator + storeName + "." + segmentId * (retentionPeriod / (NUM_SEGMENTS - 1)));
        assertTrue(newSegment.exists());
    }
}
Also used : SimpleTimeZone(java.util.SimpleTimeZone) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 30 with SimpleTimeZone

use of java.util.SimpleTimeZone in project javarosa by opendatakit.

the class DateUtilsTests method testParseTime_with_DST.

public void testParseTime_with_DST() throws Exception {
    Locale.setDefault(Locale.US);
    TimeZone backupZone = TimeZone.getDefault();
    // this is a timezone that operates DST every day of the year!
    SimpleTimeZone dstTimezone = new SimpleTimeZone(2 * 60 * 60 * 1000, "Europe/Athens", Calendar.JANUARY, 1, 0, 0, SimpleTimeZone.UTC_TIME, Calendar.DECEMBER, 31, 0, 24 * 60 * 60 * 1000, SimpleTimeZone.UTC_TIME, 60 * 60 * 1000);
    TimeZone.setDefault(dstTimezone);
    String time = "12:03:05.000Z";
    testTime(time, 43385000L);
    Date date = DateUtils.parseTime(time);
    DateFormat formatter = DateFormat.getTimeInstance();
    String formatted = formatter.format(date);
    // It should shift 3 hours, 2 for the zone and 1 for DST.
    assertEquals("3:03:05 PM", formatted);
    TimeZone.setDefault(backupZone);
}
Also used : TimeZone(java.util.TimeZone) SimpleTimeZone(java.util.SimpleTimeZone) SimpleTimeZone(java.util.SimpleTimeZone) DateFormat(java.text.DateFormat) Date(java.util.Date)

Aggregations

SimpleTimeZone (java.util.SimpleTimeZone)163 TimeZone (java.util.TimeZone)53 SimpleDateFormat (java.text.SimpleDateFormat)47 GregorianCalendar (java.util.GregorianCalendar)43 Date (java.util.Date)38 Calendar (java.util.Calendar)19 Test (org.junit.Test)16 DateFormat (java.text.DateFormat)8 File (java.io.File)5 NativeTimeZoneTest.isNativeTimeZone (com.google.j2objc.util.NativeTimeZoneTest.isNativeTimeZone)4 IOException (java.io.IOException)4 BasicDBObject (com.mongodb.BasicDBObject)3 InvalidObjectException (java.io.InvalidObjectException)3 Timestamp (java.sql.Timestamp)3 ParsePosition (java.text.ParsePosition)3 COSString (org.apache.pdfbox.cos.COSString)3 Support_TimeZone (tests.support.Support_TimeZone)3 DBRef (com.mongodb.DBRef)2 MessageFormat (java.text.MessageFormat)2 ParseException (java.text.ParseException)2