Java Code Examples for java.text.SimpleDateFormat#setCalendar()

The following examples show how to use java.text.SimpleDateFormat#setCalendar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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));
    }
}
 
Example 2
Source File: DateConverter.java    From PdfBox-Android with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: DateConverter.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: DateConverter.java    From sambox with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: CourseSectionImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
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());
}
 
Example 6
Source File: CourseSectionImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
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());
}
 
Example 7
Source File: DumpEditor.java    From MifareClassicTool with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: DumpEditor.java    From MifareClassicTool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: DateAxisTest.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 10
Source File: UmmalquraDateFormatTests.java    From ummalqura-calendar with MIT License 5 votes vote down vote up
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;
}
 
Example 11
Source File: SimpleFormatter.java    From Time4A with Apache License 2.0 5 votes vote down vote up
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;

}
 
Example 12
Source File: City.java    From sms-ticket with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: DateAxisTest.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 14
Source File: KeyEditor.java    From MifareClassicTool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: UploadListTextFile.java    From neembuu-uploader with GNU General Public License v3.0 4 votes vote down vote up
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
}
 
Example 16
Source File: DateToolTests.java    From velocity-tools with Apache License 2.0 4 votes vote down vote up
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));
}
 
Example 17
Source File: UmmalquraDateFormatTests.java    From ummalqura-calendar with MIT License 4 votes vote down vote up
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());
}
 
Example 18
Source File: TwitterAdUtil.java    From twitter4j-ads with MIT License 4 votes vote down vote up
@Override
protected SimpleDateFormat initialValue() {
    SimpleDateFormat rv = new SimpleDateFormat(FORMAT_YYYYMMDD_HHMM);
    rv.setCalendar(UTC_CALENDAR.get());
    return rv;
}
 
Example 19
Source File: MessageSearches.java    From james-project with Apache License 2.0 4 votes vote down vote up
private String createDateString(Date date, DateResolution dateResolution) {
    SimpleDateFormat format = createFormat(dateResolution);
    format.setCalendar(getGMT());
    return format.format(date);
}
 
Example 20
Source File: DigitalWatchFaceService.java    From wear-os-samples with Apache License 2.0 4 votes vote down vote up
private void initFormats() {
    mDayOfWeekFormat = new SimpleDateFormat("EEEE", Locale.getDefault());
    mDayOfWeekFormat.setCalendar(mCalendar);
    mDateFormat = DateFormat.getDateFormat(DigitalWatchFaceService.this);
    mDateFormat.setCalendar(mCalendar);
}