2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.test.java.text;
20 import gov.nasa.jpf.util.test.TestJPF;
22 import java.text.DateFormat;
23 import java.text.ParseException;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.GregorianCalendar;
27 import java.util.TimeZone;
29 import org.junit.Test;
31 public class DateFormatTest extends TestJPF {
34 public void testConversionCycle() {
35 if (verifyNoPropertyViolation()) {
36 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
40 System.out.print("current date is: ");
41 System.out.println(d1);
43 String s = df.format(d1);
44 System.out.print("formatted date is: ");
45 System.out.println(s);
48 Date d2 = df.parse(s);
50 System.out.print("re-parsed date is: ");
51 System.out.println(d2);
53 long t1 = d1.getTime(); // in ms
54 long t2 = d2.getTime(); // in ms
55 long delta = Math.abs(t2 - t1);
57 // since we loose the ms in String conversion, d2.after(d1) does not necessarily hold
58 // some locales don't format the seconds, to we might loose them in the re-conversion
59 assert delta <= 60000 : "difference > 1min";
61 } catch (ParseException x) {
62 assert false : "output did not parse: " + x;
68 public void testSetAndGetTimeZone() {
69 if (verifyNoPropertyViolation()) {
70 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
71 TimeZone timeZone = TimeZone.getTimeZone("GMT");
72 df.setTimeZone(timeZone);
73 assertEquals(timeZone, df.getTimeZone());
74 timeZone = TimeZone.getTimeZone("PDT");
75 df.setTimeZone(timeZone);
76 assertEquals(timeZone, df.getTimeZone());
81 public void testFormatWithTimeZone() {
82 if (verifyNoPropertyViolation()) {
83 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
84 TimeZone timeZone = TimeZone.getTimeZone("GMT");
85 df.setTimeZone(timeZone);
86 Calendar calendar = new GregorianCalendar(timeZone);
87 calendar.set(2010, 10, 10, 10, 10, 10);
88 String time = "10:10"; // some Locales don't include the seconds
89 String dft = df.format(calendar.getTime());
90 assertTrue(dft.contains(time));
91 df.setTimeZone(TimeZone.getTimeZone("EST"));
92 time = "5:10"; // some Locales don't include the seconds
93 dft = df.format(calendar.getTime());
94 assertTrue(dft.contains(time));