Java Code Examples for java.util.Date#getMonth()

The following examples show how to use java.util.Date#getMonth() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@ObjectiveCName("formatShortDate:")
public String formatShortDate(long date) {
    // Not using Calendar for GWT
    long delta = new Date().getTime() - date;
    if (delta < 60 * 1000) {
        return get("language.format.time.now");
    } else if (delta < 60 * 60 * 1000) {
        return get("language.format.time.minutes", SHORT)
                .replace("{minutes}", "" + delta / 60000);
    } else if (delta < 24 * 60 * 60 * 1000) {
        return get("language.format.time.hours", SHORT)
                .replace("{hours}", "" + delta / 3600000);
    } else if (delta < 2 * 24 * 60 * 60 * 1000) {
        return get("language.format.time.yesterday", SHORT);
    } else {
        // Not using Calendar for GWT
        Date date1 = new Date(date);
        int month = date1.getMonth();
        int d = date1.getDate();
        return d + " " + get(MONTHS[month], COMPACT);
    }
}
 
Example 2
Source Project: mumu   File: DateUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 取季度
 * 
 * @param date
 * @return
 */
@SuppressWarnings("deprecation")
public static int getQuarter(Date date) {
	if (date.getMonth() == 0 || date.getMonth() == 1 || date.getMonth() == 2) {
		return 1;
	} else if (date.getMonth() == 3 || date.getMonth() == 4 || date.getMonth() == 5) {
		return 2;
	} else if (date.getMonth() == 6 || date.getMonth() == 7 || date.getMonth() == 8) {
		return 3;
	} else if (date.getMonth() == 9 || date.getMonth() == 10 || date.getMonth() == 11) {
		return 4;
	} else {
		return 0;

	}
}
 
Example 3
Source Project: java   File: Human.java    License: MIT License 6 votes vote down vote up
public int getAge(){
	Date today = new Date();
	
	//the current year represented by this date, minus 1900
	int currentYear = today.getYear() + 1900;
	int age = currentYear - yearBirth;
	
	//0 is January, so add 1
	int currentMonth = today.getMonth() + 1;
	if(currentMonth < monthBirth){
		age--;
	}else if(currentMonth == monthBirth){
		//day of the Month
		int currentDay = today.getDate();
		if(dayBirth > currentDay){
			age--;
		}
	}
	return age;
}
 
Example 4
Source Project: java-n-IDE-for-Android   File: ZioEntry.java    License: Apache License 2.0 6 votes vote down vote up
public void setTime(long time) {
    Date d = new Date(time);
    long dtime;
    int year = d.getYear() + 1900;
    if (year < 1980) {
        dtime = (1 << 21) | (1 << 16);
    }
    else {
        dtime = (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
        d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
        d.getSeconds() >> 1;
    }

    modificationDate = (short)(dtime >> 16);
    modificationTime = (short)(dtime & 0xFFFF);
}
 
Example 5
public static String[] get6days(boolean returnString)
{
    Date d=getToday();

    String [] days=new String[6];
    for (int i=0;i<6;i++)
    {
        Date t=add(d,i-2);
        days[i]=t.getMonth()+1+"."+t.getDate();
    }
    return days;
}
 
Example 6
/**
* Get the order ID
* @return
*/
private String getOrderID() {
  Date d = new Date();
  return String.valueOf((d.getYear() + 1900)) + (d.getMonth() < 10 ? "0" + 
  		(d.getMonth() + 1) : (d.getMonth() + 1)) + 
  		(d.getDate() < 10 ? "0" + d.getDate() : d.getDate()) + 
  		"-" + MER_ID + "-" + System.currentTimeMillis();
}
 
Example 7
Source Project: JDKSourceCode1.8   File: ZipUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Converts Java time to DOS time.
 */
@SuppressWarnings("deprecation") // Use of date methods
private static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return ZipEntry.DOSTIME_BEFORE_1980;
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 8
private static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return 2162688L;
    }
    return year - 1980 << 25 | d.getMonth() + 1 << 21 |
            d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
            d.getSeconds() >> 1;
}
 
Example 9
@SuppressWarnings("deprecation")
public static void main(String args[])
{
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 1997);
    cal.set(Calendar.DAY_OF_YEAR, 1);
    Date d = cal.getTime();             // Should be Jan 1
    if (d.getMonth() != 0 || d.getDate() != 1) {
        throw new RuntimeException("Date isn't Jan 1");
    }
}
 
Example 10
@Override
@Cacheable(value = ARTICLES_CACHE_NAME, key = ARTICLES_CACHE_KEY)
public List<ArchiveBo> archives() {
	// 查询文章表各个时间段的文章数量 分别为DATE->时间段 count->文章数量
	List<ArchiveBo> listforArchiveBo = articleMapperCustom.findDateAndCount();
	if (listforArchiveBo != null) {
		for (ArchiveBo archiveBo : listforArchiveBo) {
			ArticleExample example = new ArticleExample();
			// 在EXAMPLE对象中存放article_status和article_post
			ArticleExample.Criteria criteria = example.createCriteria().andArticleStatusEqualTo(0)
					.andArticlePostEqualTo("post");
			example.setOrderByClause("article_newstime desc");
			String date = archiveBo.getDate();
			Date sd = DateUtil.parse(date, "yyyy年MM月");
			// 在criteria对象中放入article_newstime大于或者等于的值
			criteria.andArticleNewstimeGreaterThanOrEqualTo(sd);
			Calendar cal = Calendar.getInstance();
			// 判断获取的时间的月份是否小于12
			if (sd.getMonth() < 12) {
				cal.setTime(sd);
				// 月份 +1
				cal.add(Calendar.MONTH, +1);
			} else {
				cal.setTime(sd);
				// 年 +1
				cal.add(Calendar.YEAR, +1);
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
			String date2 = sdf.format(cal.getTime());
			Date date3 = DateUtil.parse(date2, "yyyy年MM月");
			// 在criteria对象中放入article_newstime小于的值
			criteria.andArticleNewstimeLessThan(date3);
			List<Article> articles = articleMapper.selectByExample(example);
			archiveBo.setArticles(articles);
		}
	}
	return listforArchiveBo;
}
 
Example 11
Source Project: stynico   File: HelpsMainAdapter.java    License: MIT License 5 votes vote down vote up
private String getCreateTimes(String dates)
   {
       Date old = toDate(dates);
       Date nowtime = new Date(System.currentTimeMillis());
       long values = nowtime.getTime() - old.getTime();
       values = values / 1000;
// Log.e(TAG, "====values  time===" + values);
       if (values < 60 && values > 0)
{
           return  values + "秒前";
       }
       if (values > 60 && values < 60 * 60)
{
           return values / 60 + "分钟前";
       }
       if (values < 60 * 60 * 24 && values > 60 * 60)
{
           return values / 3600 + "小时前";
       }
       if (values < 60 * 60 * 24 * 2 && values > 60 * 60 * 24)
{
           return "昨天";
       }
       if (values < 60 * 60 * 3 * 24 && values > 60 * 60 * 24 * 2)
{
           return  "前天";
       }
       if (values < 60 * 60 * 24 * 30 && values > 60 * 60 * 24 * 3)
{
           return  values / (60 * 60 * 24) + "天前";
       }
       if (values < 60 * 60 * 24 * 365 && values > 60 * 60 * 24 * 30)
{
    return nowtime.getMonth() - old.getMonth() + "个月前";
       }
       return  values / (60 * 60 * 24 * 30 * 365) + "年前";
   }
 
Example 12
Source Project: Java8CN   File: ZipUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Converts Java time to DOS time.
 */
@SuppressWarnings("deprecation") // Use of date methods
private static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return ZipEntry.DOSTIME_BEFORE_1980;
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 13
Source Project: jdk8u60   File: ZipUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 14
Source Project: tapchat-android   File: BufferEventItem.java    License: Apache License 2.0 5 votes vote down vote up
public boolean isSameDay(BufferEventItem otherItem) {
    Date date = mMessage.getDate();
    Date otherDate = otherItem.getMessage().getDate();
    return (date == null || otherDate == null) ||
        (date.getYear()  == otherDate.getYear() &&
         date.getMonth() == otherDate.getMonth() &&
         date.getDate()  == otherDate.getDate());
}
 
Example 15
Source Project: styT   File: SampleAdapter.java    License: Apache License 2.0 5 votes vote down vote up
private String getCreateTimes(String dates) {
    Date old = toDate(dates);
    Date nowtime = new Date(System.currentTimeMillis());
    long values = nowtime.getTime() - old.getTime();
    values = values / 1000;
    // Log.e(TAG, "====values  time===" + values);
    if (values < 60 && values > 0) {
        return values + "秒前";
    }
    if (values > 60 && values < 60 * 60) {
        return values / 60 + "分钟前";
    }
    if (values < 60 * 60 * 24 && values > 60 * 60) {
        return values / 3600 + "小时前";
    }
    if (values < 60 * 60 * 24 * 2 && values > 60 * 60 * 24) {
        return "昨天";
    }
    if (values < 60 * 60 * 3 * 24 && values > 60 * 60 * 24 * 2) {
        return "前天";
    }
    if (values < 60 * 60 * 24 * 30 && values > 60 * 60 * 24 * 3) {
        return values / (60 * 60 * 24) + "天前";
    }
    if (values < 60 * 60 * 24 * 365 && values > 60 * 60 * 24 * 30) {
        return nowtime.getMonth() - old.getMonth() + "个月前";
    }
    return values / (60 * 60 * 24 * 30 * 365) + "年前";
}
 
Example 16
Source Project: calendar-component   File: VCalendar.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Re-render the week grid
 *
 * @param days
 *            The days
 * @param today
 *            Todays date
 * @param realDayNames
 *            The names of the dates
 */
@SuppressWarnings("deprecation")
public void updateWeekGrid(List<CalendarDay> days, Date today, String[] realDayNames) {

    weekGrid.setFirstHour(getFirstHourOfTheDay());
    weekGrid.setLastHour(getLastHourOfTheDay());
    weekGrid.getTimeBar().updateTimeBar(is24HFormat());

    dayToolbar.clear();
    dayToolbar.addBackButton();
    dayToolbar.setVerticalSized(isHeightUndefined);
    dayToolbar.setHorizontalSized(isWidthUndefined);

    weekGrid.clearDates();
    weekGrid.setDisabled(isDisabled());

    for (CalendarDay day : days) {

        Date date = day.getDate();

        int dayOfWeek = day.getDayOfWeek();

        if (dayOfWeek < getFirstDayNumber() || dayOfWeek > getLastDayNumber()) {
            continue;
        }

        boolean isToday = false;
        int dayOfMonth = date.getDate();
        if (today.getDate() == dayOfMonth
                && today.getYear() == date.getYear()
                && today.getMonth() == date.getMonth()) {
            isToday = true;
        }

        dayToolbar.add(realDayNames[dayOfWeek - 1], date, day.getLocalizedDateFormat(), isToday ? "today" : null);
        weeklyLongEvents.addDate(date);
        weekGrid.addDate(date, day.getStyledSlots());

        if (isToday) {
            weekGrid.setToday(date, today);
        }
    }

    dayToolbar.addNextButton();
}
 
Example 17
public static void main(String[] args) {
    TimeZone defaultTimeZone = TimeZone.getDefault();
    int errors = 0;

    Calendar cal = new GregorianCalendar(BEGIN_YEAR, MARCH, 1);
    String[] tzids = TimeZone.getAvailableIDs();
    try {
        for (String id : tzids) {
            TimeZone tz = TimeZone.getTimeZone(id);
            if (!tz.useDaylightTime()) {
                continue;
            }
            TimeZone.setDefault(tz);

          dateloop:
            // Use future dates because sun.util.calendar.ZoneInfo
            // delegates offset transition calculations to a SimpleTimeZone
            // (after 2038 as of JDK7).
            for (int year = BEGIN_YEAR; year < END_YEAR; year++) {
                for (int month = MARCH; month <= NOVEMBER; month++) {
                    cal.set(year, month, 1, 15, 0, 0);
                    int maxDom = cal.getActualMaximum(DAY_OF_MONTH);
                    for (int dom = 1; dom <= maxDom; dom++) {
                        Date date = new Date(year - 1900, month, dom);
                        if (date.getYear()+1900 != year
                            || date.getMonth() != month
                            || date.getDate() != dom) {
                            System.err.printf("%s: got %04d-%02d-%02d, expected %04d-%02d-%02d%n",
                                              id,
                                              date.getYear() + 1900,
                                              date.getMonth() + 1,
                                              date.getDate(),
                                              year,
                                              month + 1,
                                              dom);
                            errors++;
                            break dateloop;
                        }
                    }
                }
            }
        }
    } finally {
        // Restore the default TimeZone.
        TimeZone.setDefault(defaultTimeZone);
    }
    if (errors > 0) {
        throw new RuntimeException("Transition test failed");
    }
}
 
Example 18
Source Project: flow-android   File: CalendarHelper.java    License: MIT License 4 votes vote down vote up
public static String getSeason( Date date ) {
    return seasons[ date.getMonth() ];
}
 
Example 19
public static CalDate toRPCDateTime(Date date) {
    return new CalDate(date.getYear() + 1900, date.getMonth() + 1, date.getDate(),
            new CalTime(date.getHours(), date.getMinutes(), date.getSeconds()));
}
 
Example 20
@DesireWarning("DMI_BAD_MONTH")
void bug2(Date date) {
    boolean b = date.getMonth() == 12;
}