Java Code Examples for java.text.DateFormat.setCalendar()

The following are Jave code examples for showing how to use setCalendar() of the java.text.DateFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: rotabuilder   File: ShiftDate.java   Source Code and License Vote up 7 votes
public String determineNextDateString() {
    TimeZone LOCAL_TIMEZONE = TimeZone.getTimeZone("GMT");
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(LOCAL_TIMEZONE);
    calendar.clear();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setCalendar(calendar);
    Date date;
    try {
        date = dateFormat.parse(dateString);
    } catch (ParseException e) {
        throw new IllegalStateException("Could not parse shiftDate (" + this
                + ")'s dateString (" + dateString + ").");
    }
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    return dateFormat.format(calendar.getTime());
}
 
Example 2
Project: mousetodon   File: DetToot.java   Source Code and License Vote up 6 votes
public String getStr() {

        String attrs="";
        if (boosted) attrs+="♡";
        if (medias.size()>0) attrs+="♭";
        if (parentid>=0) attrs+="↑";

        if (username!=null) attrs+=username;
        if (date.length()>0) {
            Calendar c = ISO8601.toCalendar(date);
            TimeZone tz = TimeZone.getDefault();
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            formatter.setCalendar(c);
            formatter.setTimeZone(tz); // convert the date to the current user
            String datestr = formatter.format(c.getTime());
            attrs+=" <font color='#EE0000'>"+datestr+"</font>";
        }
        if (lang!=null) attrs+=" ("+lang+")";
        if (attrs.length()>0) return "<p>"+attrs+"</p>"+txt.trim();
        else return txt.trim();
    }
 
Example 3
Project: calcite-avatica   File: DateTimeUtils.java   Source Code and License Vote up 6 votes
/**
 * Parses a string using {@link SimpleDateFormat} and a given pattern. This
 * method parses a string at the specified parse position and if successful,
 * updates the parse position to the index after the last character used.
 * The parsing is strict and requires months to be less than 12, days to be
 * less than 31, etc.
 *
 * @param s       string to be parsed
 * @param dateFormat Date format
 * @param tz      time zone in which to interpret string. Defaults to the Java
 *                default time zone
 * @param pp      position to start parsing from
 * @return a Calendar initialized with the parsed value, or null if parsing
 * failed. If returned, the Calendar is configured to the GMT time zone.
 */
private static Calendar parseDateFormat(String s, DateFormat dateFormat,
    TimeZone tz, ParsePosition pp) {
  if (tz == null) {
    tz = DEFAULT_ZONE;
  }
  Calendar ret = Calendar.getInstance(tz, Locale.ROOT);
  dateFormat.setCalendar(ret);
  dateFormat.setLenient(false);

  final Date d = dateFormat.parse(s, pp);
  if (null == d) {
    return null;
  }
  ret.setTime(d);
  ret.setTimeZone(UTC_ZONE);
  return ret;
}
 
Example 4
Project: openjdk-jdk10   File: CalendarRegression.java   Source Code and License Vote up 5 votes
/**
 * Week of year is wrong at the start and end of the year.
 */
public void Test4197699() {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setFirstDayOfWeek(MONDAY);
    cal.setMinimalDaysInFirstWeek(4);
    DateFormat fmt = new SimpleDateFormat("E dd MMM yyyy  'DOY='D 'WOY='w");
    fmt.setCalendar(cal);

    int[] DATA = {
        2000, JANUARY, 1, 52,
        2001, DECEMBER, 31, 1};

    for (int i = 0; i < DATA.length;) {
        cal.set(DATA[i++], DATA[i++], DATA[i++]);
        int expWOY = DATA[i++];
        int actWOY = cal.get(WEEK_OF_YEAR);
        if (expWOY == actWOY) {
            logln("Ok: " + fmt.format(cal.getTime()));
        } else {
            errln("FAIL: " + fmt.format(cal.getTime())
                    + ", expected WOY=" + expWOY);
            cal.add(DATE, -8);
            for (int j = 0; j < 14; ++j) {
                cal.add(DATE, 1);
                logln(fmt.format(cal.getTime()));
            }
        }
    }
}
 
Example 5
Project: openjdk-jdk10   File: CalendarRegression.java   Source Code and License Vote up 4 votes
/**
 * Calendar.getActualMaximum(YEAR) works wrong.
 *
 * Note: Before 1.5, this test case assumed that
 * setGregorianChange didn't change object's date. But it was
 * changed. See 4928615.
 */
public void Test4167060() {
    int field = YEAR;
    DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy G",
            Locale.US);

    int[][] dates = {
        // year, month, day of month
        {100, NOVEMBER, 1},
        {-99 /*100BC*/, JANUARY, 1},
        {1996, FEBRUARY, 29}};

    String[] id = {"Hybrid", "Gregorian", "Julian"};

    for (int k = 0; k < 3; ++k) {
        logln("--- " + id[k] + " ---");

        for (int j = 0; j < dates.length; ++j) {
            GregorianCalendar calendar = new GregorianCalendar();
            if (k == 1) {
                calendar.setGregorianChange(new Date(Long.MIN_VALUE));
            } else if (k == 2) {
                calendar.setGregorianChange(new Date(Long.MAX_VALUE));
            }
            calendar.set(dates[j][0], dates[j][1], dates[j][2]);
            format.setCalendar((Calendar) calendar.clone());

            Date dateBefore = calendar.getTime();

            int maxYear = calendar.getActualMaximum(field);
            logln("maxYear: " + maxYear + " for " + format.format(calendar.getTime()));
            logln("date before: " + format.format(dateBefore));

            int[] years = {2000, maxYear - 1, maxYear, maxYear + 1};

            for (int i = 0; i < years.length; i++) {
                boolean valid = years[i] <= maxYear;
                calendar.set(field, years[i]);
                Date dateAfter = calendar.getTime();
                int newYear = calendar.get(field);
                calendar.setTime(dateBefore); // restore calendar for next use

                logln(" Year " + years[i] + (valid ? " ok " : " bad")
                        + " => " + format.format(dateAfter));
                if (valid && newYear != years[i]) {
                    errln("  FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
                } else if (!valid && newYear == years[i]) {
                    errln("  FAIL: " + newYear + " should be invalid");
                }
            }
        }
    }
}