use of java.util.SimpleTimeZone in project head by mifos.
the class FiscalCalendarRulesTest method testIsWorkingDay.
@Test
public void testIsWorkingDay() {
String configWorkingDays = "MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY";
setNewWorkingDays(configWorkingDays);
// get the supported ids for GMT-08:00 (Pacific Standard Time)
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
// if no ids were returned, something is wrong. get out.
// Otherwise get an id for Pacific Standard Time
String pstId = ids[0];
// create a Pacific Standard Time time zone
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, pstId);
// set up rules for daylight savings time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// create a GregorianCalendar with the Pacific Daylight time zone
// and the current date and time
Calendar calendar = new GregorianCalendar(pdt);
try {
Locale savedLocale = Localization.getInstance().getConfiguredLocale();
new LocalizationConverter().setCurrentLocale(Locale.US);
SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
// Keith: DateFormat must be set to the same timezone as the
// calendar
// Otherwise dates don't roll over at the same exact time, causing
// this and several other unit tests to fail
df.setTimeZone(TimeZone.getTimeZone(pstId));
df.applyPattern("yyyy/MM/dd");
Date thursday = df.parse("2007/10/11");
calendar.setTime(thursday);
String out = thursday.toString();
out.contains("A");
new LocalizationConverter().setCurrentLocale(savedLocale);
} catch (Exception e) {
}
Assert.assertTrue(new FiscalCalendarRules().isWorkingDay(calendar));
// Friday
calendar.add(Calendar.DAY_OF_WEEK, 1);
Assert.assertTrue(new FiscalCalendarRules().isWorkingDay(calendar));
// Sat
calendar.add(Calendar.DAY_OF_WEEK, 1);
Assert.assertTrue(!new FiscalCalendarRules().isWorkingDay(calendar));
// Sunday
calendar.add(Calendar.DAY_OF_WEEK, 1);
Assert.assertTrue(!new FiscalCalendarRules().isWorkingDay(calendar));
}
use of java.util.SimpleTimeZone in project mongo-java-driver by mongodb.
the class JSONTest method testDate.
@Test
public void testDate() {
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
String formattedDate = format.format(d);
String serialized = JSON.serialize(d);
assertEquals("{ \"$date\" : \"" + formattedDate + "\"}", serialized);
Date d2 = (Date) JSON.parse(serialized);
assertEquals(d2.toString(), d.toString());
assertTrue(d.equals(d2));
}
use of java.util.SimpleTimeZone in project mongo-java-driver by mongodb.
the class JSONTest method testJSONEncoding.
@Test
public void testJSONEncoding() throws ParseException {
String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , " + "'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , " + "'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , " + "'$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , " + "'codews' : { '$code' : 'ggggg' , " + "'$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' : null, " + "'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
BasicDBObject a = (BasicDBObject) JSON.parse(json);
assertEquals("asdfasd", a.get("str"));
assertEquals(5, a.get("int"));
assertEquals(123123123123L, a.get("long"));
assertEquals(0.4d, a.get("float"));
assertEquals(false, a.get("bool"));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
assertEquals(format.parse("2011-05-18T18:56:00Z"), a.get("date"));
Pattern pat = (Pattern) a.get("pat");
Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
assertEquals(pat.pattern(), pat2.pattern());
assertEquals(pat.flags(), pat2.flags());
ObjectId oid = (ObjectId) a.get("oid");
assertEquals(new ObjectId("4d83ab3ea39562db9c1ae2ae"), oid);
// DBRef ref = (DBRef) a.get("ref");
// assertEquals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af")), ref);
assertEquals(new Code("asdfdsa"), a.get("code"));
assertEquals(new CodeWScope("ggggg", new BasicBSONObject()), a.get("codews"));
assertEquals(new BSONTimestamp(1300474885, 10), a.get("ts"));
assertEquals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5"), a.get("uuid"));
String json2 = JSON.serialize(a);
BasicDBObject b = (BasicDBObject) JSON.parse(json2);
assertEquals(JSON.serialize(b), JSON.serialize(b));
}
use of java.util.SimpleTimeZone in project jdk8u_jdk by JetBrains.
the class ZoneInfoOld method inDaylightTime.
/**
* Queries if the specified date is in Daylight Saving Time.
*/
public boolean inDaylightTime(Date date) {
if (date == null) {
throw new NullPointerException();
}
if (transitions == null) {
return false;
}
long utc = date.getTime() - rawOffsetDiff;
int index = getTransitionIndex(utc, UTC_TIME);
// before transitions in the transition table
if (index < 0) {
return false;
}
// the time is in the table range.
if (index < transitions.length) {
return (transitions[index] & DST_MASK) != 0;
}
// beyond the transition table
SimpleTimeZone tz = getLastRule();
if (tz != null) {
return tz.inDaylightTime(date);
}
return false;
}
use of java.util.SimpleTimeZone in project jdk8u_jdk by JetBrains.
the class ZoneInfoOld method getOffsets.
private int getOffsets(long date, int[] offsets, int type) {
// if dst is never observed, there is no transition.
if (transitions == null) {
int offset = getLastRawOffset();
if (offsets != null) {
offsets[0] = offset;
offsets[1] = 0;
}
return offset;
}
date -= rawOffsetDiff;
int index = getTransitionIndex(date, type);
// FIXME: should support LMT.
if (index < 0) {
int offset = getLastRawOffset();
if (offsets != null) {
offsets[0] = offset;
offsets[1] = 0;
}
return offset;
}
if (index < transitions.length) {
long val = transitions[index];
int offset = this.offsets[(int) (val & OFFSET_MASK)] + rawOffsetDiff;
if (offsets != null) {
int dst = (int) ((val >>> DST_NSHIFT) & 0xfL);
int save = (dst == 0) ? 0 : this.offsets[dst];
offsets[0] = offset - save;
offsets[1] = save;
}
return offset;
}
// beyond the transitions, delegate to SimpleTimeZone if there
// is a rule; otherwise, return rawOffset.
SimpleTimeZone tz = getLastRule();
if (tz != null) {
int rawoffset = tz.getRawOffset();
long msec = date;
if (type != UTC_TIME) {
msec -= rawOffset;
}
int dstoffset = tz.getOffset(msec) - rawOffset;
// Check if it's in a standard-to-daylight transition.
if (dstoffset > 0 && tz.getOffset(msec - dstoffset) == rawoffset) {
dstoffset = 0;
}
if (offsets != null) {
offsets[0] = rawoffset;
offsets[1] = dstoffset;
}
return rawoffset + dstoffset;
}
int offset = getLastRawOffset();
if (offsets != null) {
offsets[0] = offset;
offsets[1] = 0;
}
return offset;
}
Aggregations