下面列出了java.util.GregorianCalendar#MONTH 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static TimePeriod toTimePeriod(double seconds) {
if ( seconds >= 36288000 ) {
return new TimePeriod( GregorianCalendar.YEAR , seconds/3024000 );
}
if ( seconds >= 3024000 ) {
return new TimePeriod( GregorianCalendar.MONTH , seconds/3024000 );
}
if ( seconds >= 604800 ) {
return new TimePeriod( WEEK_FIELD_ID , seconds/604800.0 );
}
if ( seconds >= 86400 ) {
return new TimePeriod( DAY_FIELD_ID , seconds/86400.0 );
}
if ( seconds >= 3600 ) {
return new TimePeriod( HOUR_FIELD_ID , seconds/3600.0 );
}
if (seconds >= 60) {
return new TimePeriod(GregorianCalendar.MINUTE, seconds / 60.0);
}
if (seconds >= 1) {
return new TimePeriod(GregorianCalendar.SECOND, seconds);
}
return new TimePeriod(GregorianCalendar.MILLISECOND, 1000*seconds);
}
protected int getRecurrenceType()
{
return GregorianCalendar.MONTH;
}
protected int getRecurrenceType()
{
return GregorianCalendar.MONTH;
}
public static Date parse( String s ) {
if ( s.equals( "" ))
return null;
Date date = new Date(-1);
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setLenient(false);
String dateText = s;
int dateShift = 0;
int shiftBy = GregorianCalendar.DAY_OF_YEAR;
// handles addition of negative numbers
if ( dateText.matches( ".+\\+.+" ) || dateText.matches( ".+-.+" ) ) {
String[] sp = new String[] { "", "" };
int shiftFactor = 1;
if ( dateText.matches( ".+\\+.+" )) {
sp = dateText.split( "\\+" );
shiftFactor = 1;
}
// does NOT handle subtract of negative numbers
else if ( dateText.matches( ".+-.+" ) ) {
sp = dateText.split( "-" );
shiftFactor = -1;
// if we split into two, then there was just one -
if ( sp.length != 2 )
System.err.println( "ERROR(CPSDateValidator): Can't understand date:" +
dateText + " [Too many '-'s]" );
}
// trim off leading and trailing whitespace
dateText = sp[0].trim();
String dateShiftString = sp[1].trim();
if ( dateShiftString.matches( ".+[dwmy]" )) {
// we match on w or m; trailing whitespace was already trimmed
// d isn't necessary since it's default
if ( dateShiftString.matches( ".+w" ))
shiftBy = GregorianCalendar.WEEK_OF_YEAR;
else if ( dateShiftString.matches( ".+m" ))
shiftBy = GregorianCalendar.MONTH;
else if ( dateShiftString.matches( ".+y" ))
shiftBy = GregorianCalendar.YEAR;
// trim off the trailing character (the d, w or m)
dateShiftString = dateShiftString.substring( 0, dateShiftString.length() - 1 );
}
dateShift = shiftFactor * Integer.parseInt( dateShiftString );
}
for ( Integer f : formatList ) {
String format = getFormat(f);
sdf.applyPattern( format );
try {
// if the date parses, then break the for loop
date = sdf.parse( dateText );
// System.out.println("DATE MATCHED: " + format );
// if the matched format doesn't include a year component,
// then shift the date into this year.
if ( format.indexOf("yy") == -1 ) {
GregorianCalendar c = new GregorianCalendar();
GregorianCalendar now = new GregorianCalendar();
c.setTime( date );
now.setTime( new Date() );
c.set( GregorianCalendar.YEAR, now.get( GregorianCalendar.YEAR ) );
date = c.getTime();
}
break;
}
// if the date doesn't parse, try the next pattern
catch ( Exception ignore ) {}
System.err.println( "ERROR parsing date: " + s );
}
// if dateShift is not 0, then we should add it to the parsed date
if ( dateShift != 0 ) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime( date );
cal.add( shiftBy, dateShift );
date = cal.getTime();
}
return date;
}
static void round(GregorianCalendar cal, int field) {
if (GregorianCalendar.MILLISECOND == field) {
return;
}
cal.set(GregorianCalendar.MILLISECOND, 0);
if (GregorianCalendar.SECOND == field) {
return;
}
cal.set(GregorianCalendar.SECOND, 0);
if (GregorianCalendar.MINUTE == field) {
return;
}
cal.set(GregorianCalendar.MINUTE, 0);
if ( HOUR_FIELD_ID == field ) {
return;
}
cal.set( HOUR_FIELD_ID , FIRST_HOUR );
if ( DAY_FIELD_ID == field ) {
return;
}
cal.set(DAY_FIELD_ID , FIRST_DAY );
if ( WEEK_FIELD_ID == field ) {
return;
}
//here, we are rounding down to the first week (i.e. the first day of
//the month), so the day of the week and the week of the month no
//longer matter - we just set the day to be the first day of the month
cal.set( GregorianCalendar.DAY_OF_MONTH , 1 );
if ( GregorianCalendar.MONTH == field ) {
return;
}
cal.set( GregorianCalendar.MONTH , 0 );
if ( GregorianCalendar.YEAR == field ) {
return;
}
cal.set( GregorianCalendar.YEAR , 0 );
}