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

The following examples show how to use java.util.Date#getMonth() . 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: IntlEngine.java    From actor-platform with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 File: DateUtils.java    From mumu with 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 File: Human.java    From java with 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 File: ZioEntry.java    From java-n-IDE-for-Android with 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
Source File: PaymentCardShenZhouFu.java    From gameserver with Apache License 2.0 5 votes vote down vote up
/**
* 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 6
Source File: SampleAdapter.java    From styT with 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 7
Source File: BufferEventItem.java    From tapchat-android with 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 8
Source File: ZipUtils.java    From jdk8u60 with 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 9
Source File: ZipUtils.java    From Java8CN with 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 10
Source File: HelpsMainAdapter.java    From stynico with 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 11
Source File: ArticleServiceImpl.java    From mayday with GNU General Public License v3.0 5 votes vote down vote up
@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 12
Source File: bug4100311.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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 13
Source File: ZipEntry.java    From appcan-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 14
Source File: ZipUtils.java    From JDKSourceCode1.8 with 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 15
Source File: WeatherChartView.java    From healthgo with GNU General Public License v3.0 5 votes vote down vote up
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 16
Source File: CalendarHelper.java    From flow-android with MIT License 4 votes vote down vote up
public static String getSeason( Date date ) {
    return seasons[ date.getMonth() ];
}
 
Example 17
Source File: Bug6772689.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
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 File: DateConstants.java    From calendar-component with Apache License 2.0 4 votes vote down vote up
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 19
Source File: VCalendar.java    From calendar-component with 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 20
Source File: DMI_BAD_MONTH.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@DesireWarning("DMI_BAD_MONTH")
void bug2(Date date) {
    boolean b = date.getMonth() == 12;
}