下面列出了java.util.GregorianCalendar#setTimeZone ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Some checks for the getFirstMillisecond(TimeZone) method.
*/
public void testGetFirstMillisecondWithCalendar() {
Week w = new Week(1, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(978307200000L, w.getFirstMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
w.getFirstMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getFirstMillisecond(TimeZone) method.
*/
public void testGetFirstMillisecondWithCalendar() {
Year y = new Year(2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(978307200000L, y.getFirstMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
y.getFirstMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getLastMillisecond(TimeZone) method.
*/
@Test
public void testGetLastMillisecondWithCalendar() {
Quarter q = new Quarter(3, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(1001894399999L, q.getLastMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
q.getLastMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getFirstMillisecond(TimeZone) method.
*/
@Test
public void testGetFirstMillisecondWithCalendar() {
Millisecond m = new Millisecond(500, 55, 40, 2, 15, 4, 2000);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(955766455500L, m.getFirstMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
m.getFirstMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getLastMillisecond(TimeZone) method.
*/
@Test
public void testGetLastMillisecondWithCalendar() {
Second s = new Second(50, 45, 21, 21, 4, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(987889550999L, s.getLastMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
s.getLastMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getFirstMillisecond(TimeZone) method.
*/
@Test
public void testGetFirstMillisecondWithCalendar() {
Day d = new Day(1, 12, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(1007164800000L, d.getFirstMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
d.getFirstMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getLastMillisecond(TimeZone) method.
*/
@Test
public void testGetLastMillisecondWithCalendar() {
Minute m = new Minute(45, 21, 21, 4, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(987889559999L, m.getLastMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
m.getLastMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getFirstMillisecond(TimeZone) method.
*/
@Test
public void testGetFirstMillisecondWithCalendar() {
Millisecond m = new Millisecond(500, 55, 40, 2, 15, 4, 2000);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(955766455500L, m.getFirstMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
m.getFirstMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getFirstMillisecond(TimeZone) method.
*/
public void testGetFirstMillisecondWithCalendar() {
Quarter q = new Quarter(1, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(978307200000L, q.getFirstMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
q.getFirstMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getLastMillisecond(TimeZone) method.
*/
@Test
public void testGetLastMillisecondWithCalendar() {
Minute m = new Minute(45, 21, 21, 4, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(987889559999L, m.getLastMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
m.getLastMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getFirstMillisecond(TimeZone) method.
*/
@Test
public void testGetFirstMillisecondWithCalendar() {
Day d = new Day(1, 12, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(1007164800000L, d.getFirstMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
d.getFirstMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getFirstMillisecond(TimeZone) method.
*/
@Test
public void testGetFirstMillisecondWithCalendar() {
Week w = new Week(1, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(978307200000L, w.getFirstMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
w.getFirstMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getLastMillisecond(TimeZone) method.
*/
@Test
public void testGetLastMillisecondWithCalendar() {
Hour h = new Hour(21, 21, 4, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(987890399999L, h.getLastMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
h.getLastMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the getLastMillisecond(TimeZone) method.
*/
@Test
public void testGetLastMillisecondWithCalendar() {
Week w = new Week(52, 2001);
GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
assertEquals(1009756799999L, w.getLastMillisecond(calendar));
// try null calendar
boolean pass = false;
try {
w.getLastMillisecond((Calendar) null);
}
catch (NullPointerException e) {
pass = true;
}
assertTrue(pass);
}
static GregorianCalendar newGreg()
{
GregorianCalendar retCal = new GregorianCalendar(Locale.ENGLISH);
retCal.setTimeZone(new SimpleTimeZone(0, "UTC"));
retCal.setLenient(false);
retCal.set(Calendar.MILLISECOND, 0);
return retCal;
}
@SuppressWarnings("deprecation")
public void testDateDeserializationShouldReturnExpectedEntity() throws Throwable {
final String tableName = "MyTableName";
final GregorianCalendar calendar = new GregorianCalendar(2013, 0, 22, 10, 30, 40);
calendar.setTimeZone(TimeZone.getTimeZone("GMT-4"));
final DateTestObject dateObject = new DateTestObject(calendar.getTime());
MobileServiceClient client = null;
try {
client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());
} catch (MalformedURLException e) {
e.printStackTrace();
}
client = client.withFilter(new ServiceFilter() {
@Override
public ListenableFuture<ServiceFilterResponse> handleRequest(ServiceFilterRequest request, NextServiceFilterCallback nextServiceFilterCallback) {
// Create a mock response simulating an error
ServiceFilterResponseMock response = new ServiceFilterResponseMock();
response.setStatus((new StatusLine(Protocol.HTTP_2, 404, "")));
response.setContent("{\"date\":\"2013-01-22T14:30:40.000Z\"}");
final SettableFuture<ServiceFilterResponse> resultFuture = SettableFuture.create();
resultFuture.set(response);
return resultFuture;
}
});
MobileServiceTable<DateTestObject> table = client.getTable(tableName, DateTestObject.class);
DateTestObject entity = table.insert(dateObject).get();
// Asserts
Date expctedDate = dateObject.getDate();
DateTestObject returnedDateObject = entity;
assertNotNull("DateTestObject should not be null", returnedDateObject);
Date d = returnedDateObject.getDate();
assertNotNull("Date should not be null", d);
assertEquals(expctedDate.getYear(), d.getYear());
assertEquals(expctedDate.getMonth(), d.getMonth());
assertEquals(expctedDate.getDay(), d.getDay());
assertEquals(expctedDate.getHours(), d.getHours());
assertEquals(expctedDate.getMinutes(), d.getMinutes());
assertEquals(expctedDate.getSeconds(), d.getSeconds());
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
/**
* Fill the calendar with the parsed date.
*
* @param cal calendar to fill
* @param ignoreChanges if <tt>true</tt>, throw an exception when a date
* like <tt>Sept 31</tt> is changed to <tt>Oct 1</tt>
*
* @throws CalendarParserException if the date cannot be set for some
* reason
*/
void setCalendar(GregorianCalendar cal, boolean ignoreChanges) throws CalendarParserException {
cal.clear();
if (year != UNSET && month != UNSET && day != UNSET) {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
if (!ignoreChanges) {
final int calYear = cal.get(Calendar.YEAR);
final int calMonth = cal.get(Calendar.MONTH);
final int calDay = cal.get(Calendar.DATE);
if (calYear != year || (calMonth + 1) != month ||
calDay != day)
{
throw new CalendarParserException("Date was set to " +
calYear + "/" +
(calMonth + 1) + "/" +
calDay +
" not requested " +
year + "/" + month +
"/" + day);
}
}
}
cal.clear(Calendar.HOUR);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
if (hour != UNSET && minute != UNSET) {
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.MINUTE, minute);
if (second != UNSET) {
cal.set(Calendar.SECOND, second);
if (milli != UNSET) {
cal.set(Calendar.MILLISECOND, milli);
}
}
if (timeZone != null) {
cal.setTimeZone(timeZone);
}
}
}
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}
public void testDateSerializationShouldReturnExpectedJson() throws Throwable {
// Container to store callback's results and do the asserts.
final ResultsContainer container = new ResultsContainer();
final String tableName = "MyTableName";
final GregorianCalendar date = new GregorianCalendar(2013, 0, 22, 10, 30, 40);
date.setTimeZone(TimeZone.getTimeZone("GMT-4"));
final DateTestObject dateObject = new DateTestObject(date.getTime());
MobileServiceClient client = null;
try {
client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());
} catch (MalformedURLException e) {
e.printStackTrace();
}
client = client.withFilter(new ServiceFilter() {
@Override
public ListenableFuture<ServiceFilterResponse> handleRequest(ServiceFilterRequest request, NextServiceFilterCallback nextServiceFilterCallback) {
// Store the request content
container.setRequestContent(request.getContent());
ServiceFilterResponseMock mockedResponse = new ServiceFilterResponseMock();
mockedResponse.setContent("{}");
final SettableFuture<ServiceFilterResponse> resultFuture = SettableFuture.create();
resultFuture.set(mockedResponse);
return resultFuture;
}
});
MobileServiceTable<DateTestObject> table = client.getTable(tableName, DateTestObject.class);
table.insert(dateObject).get();
// Asserts
// Date should have UTC format (+4 that date value)
assertEquals("{\"date\":\"2013-01-22T14:30:40.000Z\"}", container.getRequestContent());
}