下面列出了java.text.SimpleDateFormat#setCalendar ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void Test4061476() {
SimpleDateFormat fmt = new SimpleDateFormat("ddMMMyy", Locale.UK);
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"),
Locale.UK);
fmt.setCalendar(cal);
try {
Date date = fmt.parse("29MAY97");
cal.setTime(date);
} catch (Exception e) {
}
cal.set(HOUR_OF_DAY, 13);
logln("Hour: " + cal.get(HOUR_OF_DAY));
cal.add(HOUR_OF_DAY, 6);
logln("Hour: " + cal.get(HOUR_OF_DAY));
if (cal.get(HOUR_OF_DAY) != 19) {
errln("Fail: Want 19 Got " + cal.get(HOUR_OF_DAY));
}
}
private static GregorianCalendar parseSimpleDate(String text, String[] fmts,
ParsePosition initialWhere)
{
for(String fmt : fmts)
{
ParsePosition where = new ParsePosition(initialWhere.getIndex());
SimpleDateFormat sdf = new SimpleDateFormat(fmt, Locale.ENGLISH);
GregorianCalendar retCal = newGreg();
sdf.setCalendar(retCal);
if (sdf.parse(text, where) != null)
{
initialWhere.setIndex(where.getIndex());
skipOptionals(text, initialWhere, " ");
return retCal;
}
}
return null;
}
private static GregorianCalendar parseSimpleDate(String text, String[] fmts,
ParsePosition initialWhere)
{
for(String fmt : fmts)
{
ParsePosition where = new ParsePosition(initialWhere.getIndex());
SimpleDateFormat sdf = new SimpleDateFormat(fmt, Locale.ENGLISH);
GregorianCalendar retCal = newGreg();
sdf.setCalendar(retCal);
if (sdf.parse(text, where) != null)
{
initialWhere.setIndex(where.getIndex());
skipOptionals(text, initialWhere, " ");
return retCal;
}
}
return null;
}
public static final String convertTimeToString(Time time) {
if(time == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(CourseSectionImpl.TIME_FORMAT_DATE_TZ);
// Time zone from user
TimeZone userTz = userTimeService.getLocalTimeZone();
sdf.setTimeZone(userTz);
// Today at 0.00
Calendar date = new GregorianCalendar(userTz);
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
// Add the RawOffset of server, to write REAL TIME in STRING detached from server
date.setTimeInMillis(date.getTimeInMillis()+time.getTime()+TimeZone.getDefault().getRawOffset());
sdf.setCalendar(date);
return sdf.format(date.getTime());
}
/**
* Check if it is a valid dump ({@link #checkDumpAndUpdateLines()}),
* create a file name suggestion and call
* {@link #saveFile(String[], String, boolean, int, int)}.
* @see #checkDumpAndUpdateLines()
* @see #saveFile(String[], String, boolean, int, int)
*/
private void saveDump() {
int err = checkDumpAndUpdateLines();
if (err != 0) {
Common.isValidDumpErrorToast(err, this);
return;
}
// Set a filename (UID + Date + Time) if there is none.
if (mDumpName == null) {
GregorianCalendar calendar = new GregorianCalendar();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",
Locale.getDefault());
fmt.setCalendar(calendar);
String dateFormatted = fmt.format(calendar.getTime());
mDumpName = "UID_" + mUID + "_" + dateFormatted;
}
saveFile(mLines, mDumpName, true, R.string.dialog_save_dump_title,
R.string.dialog_save_dump);
}
public static final String convertTimeToString(Time time) {
if(time == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(CourseSectionImpl.TIME_FORMAT_DATE_TZ);
// Time zone from user
TimeZone userTz = userTimeService.getLocalTimeZone();
sdf.setTimeZone(userTz);
// Today at 0.00
Calendar date = new GregorianCalendar(userTz);
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
// Add the RawOffset of server, to write REAL TIME in STRING detached from server
date.setTimeInMillis(date.getTimeInMillis()+time.getTime()+TimeZone.getDefault().getRawOffset());
sdf.setCalendar(date);
return sdf.format(date.getTime());
}
private static GregorianCalendar parseSimpleDate(String text, String[] fmts,
ParsePosition initialWhere)
{
for (String fmt : fmts)
{
ParsePosition where = new ParsePosition(initialWhere.getIndex());
SimpleDateFormat sdf = new SimpleDateFormat(fmt, Locale.ENGLISH);
GregorianCalendar retCal = newGreg();
sdf.setCalendar(retCal);
if (sdf.parse(text, where) != null)
{
initialWhere.setIndex(where.getIndex());
skipOptionals(text, initialWhere, " ");
return retCal;
}
}
return null;
}
/**
* A test to reproduce bug 2201869.
*/
@Test
public void testBug2201869() {
TimeZone tz = TimeZone.getTimeZone("GMT");
GregorianCalendar c = new GregorianCalendar(tz, Locale.UK);
DateAxis axis = new DateAxis("Date", tz, Locale.UK);
SimpleDateFormat sdf = new SimpleDateFormat("d-MMM-yyyy", Locale.UK);
sdf.setCalendar(c);
axis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, sdf));
Day d1 = new Day(1, 3, 2008);
d1.peg(c);
Day d2 = new Day(30, 6, 2008);
d2.peg(c);
axis.setRange(d1.getStart(), d2.getEnd());
BufferedImage image = new BufferedImage(200, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, 200, 100);
axis.setTickMarkPosition(DateTickMarkPosition.END);
List ticks = axis.refreshTicks(g2, new AxisState(), area,
RectangleEdge.BOTTOM);
assertEquals(3, ticks.size());
DateTick t1 = (DateTick) ticks.get(0);
assertEquals("31-Mar-2008", t1.getText());
DateTick t2 = (DateTick) ticks.get(1);
assertEquals("30-Apr-2008", t2.getText());
DateTick t3 = (DateTick) ticks.get(2);
assertEquals("31-May-2008", t3.getText());
// now repeat for a vertical axis
ticks = axis.refreshTicks(g2, new AxisState(), area,
RectangleEdge.LEFT);
assertEquals(3, ticks.size());
t1 = (DateTick) ticks.get(0);
assertEquals("31-Mar-2008", t1.getText());
t2 = (DateTick) ticks.get(1);
assertEquals("30-Apr-2008", t2.getText());
t3 = (DateTick) ticks.get(2);
assertEquals("31-May-2008", t3.getText());
}
/**
* Share a key file as "file://" stream resource (e.g. as e-mail
* attachment). The key file will be checked and stored in the
* {@link Common#TMP_DIR} directory. After this, a dialog will be displayed
* in which the user can choose between apps that are willing to
* handle the dump.
* @see Common#TMP_DIR
* @see #isValidKeyFileErrorToast()
*
*/
private void shareKeyFile() {
if (!isValidKeyFileErrorToast()) {
return;
}
// Save key file to to a temporary file which will be
// attached for sharing (and stored in the tmp folder).
String fileName;
if (mFileName.equals("")) {
// The key file has no name. Use date and time as name.
GregorianCalendar calendar = new GregorianCalendar();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",
Locale.getDefault());
fmt.setCalendar(calendar);
fileName = fmt.format(calendar.getTime());
} else {
fileName = mFileName;
}
// Save file to tmp directory.
File file = Common.getFileFromStorage(Common.HOME_DIR + "/" +
Common.TMP_DIR + "/" + fileName);
if (!Common.saveFile(file, mLines, false)) {
Toast.makeText(this, R.string.info_save_error,
Toast.LENGTH_LONG).show();
return;
}
// Share file.
Common.shareTmpFile(this, file);
}
/**
* Share a dump as "file://" stream resource (e.g. as e-mail attachment).
* The dump will be checked and stored in the {@link Common#TMP_DIR}
* directory. After this, a dialog will be displayed in which the user
* can choose between apps that are willing to handle the dump.
* @see Common#TMP_DIR
*/
private void shareDump() {
int err = checkDumpAndUpdateLines();
if (err != 0) {
Common.isValidDumpErrorToast(err, this);
return;
}
// Save dump to to a temporary file which will be
// attached for sharing (and stored in the tmp folder).
String fileName;
if (mDumpName == null) {
// The dump has no name. Use date and time as name.
GregorianCalendar calendar = new GregorianCalendar();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",
Locale.getDefault());
fmt.setCalendar(calendar);
fileName = fmt.format(calendar.getTime());
} else {
fileName = mDumpName;
}
// Save file to tmp directory.
File file = Common.getFileFromStorage(Common.HOME_DIR + "/" +
Common.TMP_DIR + "/" + fileName);
if (!Common.saveFile(file, mLines, false)) {
Toast.makeText(this, R.string.info_save_error,
Toast.LENGTH_LONG).show();
return;
}
// Share file.
Common.shareTmpFile(this, file);
}
public SimpleDateFormat getSdfTime() {
String[] dateFormatParts = dateFormat.split(" ");
if (dateFormatParts.length > 1) {
SimpleDateFormat sdfTime = new SimpleDateFormat(dateFormatParts[1]);
sdfTime.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("Europe/Prague"), new Locale("cs", "CZ")));
return sdfTime;
}
return null;
}
private static SimpleDateFormat setUp(
String pattern,
Locale locale,
XCalendar gcal,
boolean lenient
) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
sdf.setCalendar(gcal);
sdf.setLenient(lenient);
return sdf;
}
private UmmalquraCalendar parseDate(String dateText, String fmt, Locale l)
throws ParseException {
UmmalquraCalendar uCal = new UmmalquraCalendar(l);
SimpleDateFormat dateFormat = new SimpleDateFormat(fmt, l);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dateFormat.setCalendar(uCal);
uCal.set(Calendar.YEAR, 1420);
dateFormat.set2DigitYearStart(uCal.getTime());
uCal.setTime(dateFormat.parse(dateText));
return uCal;
}
/**
* A test to reproduce bug 2201869.
*/
@Test
public void testBug2201869() {
TimeZone tz = TimeZone.getTimeZone("GMT");
GregorianCalendar c = new GregorianCalendar(tz, Locale.UK);
DateAxis axis = new DateAxis("Date", tz, Locale.UK);
SimpleDateFormat sdf = new SimpleDateFormat("d-MMM-yyyy", Locale.UK);
sdf.setCalendar(c);
axis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, sdf));
Day d1 = new Day(1, 3, 2008);
d1.peg(c);
Day d2 = new Day(30, 6, 2008);
d2.peg(c);
axis.setRange(d1.getStart(), d2.getEnd());
BufferedImage image = new BufferedImage(200, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, 200, 100);
axis.setTickMarkPosition(DateTickMarkPosition.END);
List ticks = axis.refreshTicks(g2, new AxisState(), area,
RectangleEdge.BOTTOM);
assertEquals(3, ticks.size());
DateTick t1 = (DateTick) ticks.get(0);
assertEquals("31-Mar-2008", t1.getText());
DateTick t2 = (DateTick) ticks.get(1);
assertEquals("30-Apr-2008", t2.getText());
DateTick t3 = (DateTick) ticks.get(2);
assertEquals("31-May-2008", t3.getText());
// now repeat for a vertical axis
ticks = axis.refreshTicks(g2, new AxisState(), area,
RectangleEdge.LEFT);
assertEquals(3, ticks.size());
t1 = (DateTick) ticks.get(0);
assertEquals("31-Mar-2008", t1.getText());
t2 = (DateTick) ticks.get(1);
assertEquals("30-Apr-2008", t2.getText());
t3 = (DateTick) ticks.get(2);
assertEquals("31-May-2008", t3.getText());
}
private void initFormats() {
mDayOfWeekFormat = new SimpleDateFormat("EEEE", Locale.getDefault());
mDayOfWeekFormat.setCalendar(mCalendar);
mDateFormat = DateFormat.getDateFormat(DigitalWatchFaceService.this);
mDateFormat.setCalendar(mCalendar);
}
private String createDateString(Date date, DateResolution dateResolution) {
SimpleDateFormat format = createFormat(dateResolution);
format.setCalendar(getGMT());
return format.format(date);
}
@Override
protected SimpleDateFormat initialValue() {
SimpleDateFormat rv = new SimpleDateFormat(FORMAT_YYYYMMDD_HHMM);
rv.setCalendar(UTC_CALENDAR.get());
return rv;
}
public @Test void toIsoFormat() throws Exception
{
DateTool dt = new DateTool();
dt.setLocale(TEST_LOCALE);
dt.setTimeZone(TEST_TIME_ZONE);
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TEST_TIME_ZONE);
Date date = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat();
format.setCalendar(cal);
format.applyPattern("yyyy-MM-dd'T'HH:mm:ss");
assertEquals("DateTool incorrectly formatted iso format", format.format(date), dt.format("iso", date));
format.applyPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
assertEquals("DateTool incorrectly formatted iso format", format.format(date), dt.format("iso_tz",date));
format.applyPattern("yyyy-MM-dd HH:mm:ss");
assertEquals("DateTool incorrectly formatted iso format", format.format(date), dt.format("intl",date));
format.applyPattern("yyyy-MM-dd HH:mm:ss");
assertEquals("DateTool incorrectly formatted iso format", format.format(date) + " " + TEST_TIME_ZONE.getID(), dt.format("intl_tz",date));
format.applyPattern("yyyy-MM-dd");
assertEquals("DateTool incorrectly formatted iso format", format.format(date), dt.format("iso_date",date));
format.applyPattern("yyyy-MM-dd");
assertEquals("DateTool incorrectly formatted iso format", format.format(date), dt.format("intl_date",date));
format.applyPattern("HH:mm:ss");
assertEquals("DateTool incorrectly formatted iso format",format.format(date), dt.format("iso_time",date));
format.applyPattern("HH:mm:ss");
assertEquals("DateTool incorrectly formatted iso format", format.format(date), dt.format("intl_time",date));
format.applyPattern("HH:mm:ssXXX");
assertEquals("DateTool incorrectly formatted iso format", format.format(date), dt.format("iso_tz_time",date));
format.applyPattern("HH:mm:ss");
assertEquals("DateTool incorrectly formatted iso format", format.format(date) + " " + TEST_TIME_ZONE.getID(), dt.format("intl_tz_time",date));
}
public UploadListTextFile(Path root) {
this.destination = root.resolve("successfullyUploadedFilesList.txt");
df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
df.setCalendar(Calendar.getInstance(Locale.ENGLISH)); // we don't want weird characters in date
}
private String formatDate(Calendar cal, Locale l, String fmt) {
SimpleDateFormat dateFormat = new SimpleDateFormat(fmt, l);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dateFormat.setCalendar(cal);
return dateFormat.format(cal.getTime());
}