本文整理匯總了Java中java.text.DateFormat.setCalendar方法的典型用法代碼示例。如果您正苦於以下問題:Java DateFormat.setCalendar方法的具體用法?Java DateFormat.setCalendar怎麽用?Java DateFormat.setCalendar使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.text.DateFormat
的用法示例。
在下文中一共展示了DateFormat.setCalendar方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: determineNextDateString
import java.text.DateFormat; //導入方法依賴的package包/類
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());
}
示例2: getStr
import java.text.DateFormat; //導入方法依賴的package包/類
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();
}
示例3: parseDateFormat
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 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;
}
示例4: Test4197699
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 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()));
}
}
}
}
示例5: Test4167060
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 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");
}
}
}
}
}