本文整理汇总了Java中java.util.Calendar.setTime方法的典型用法代码示例。如果您正苦于以下问题:Java Calendar.setTime方法的具体用法?Java Calendar.setTime怎么用?Java Calendar.setTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Calendar
的用法示例。
在下文中一共展示了Calendar.setTime方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDaysCode
import java.util.Calendar; //导入方法依赖的package包/类
public int getDaysCode(Event event) {
int daysCode = 0;
for (Iterator i=event.getMeetings().iterator();i.hasNext();) {
Meeting meeting = (Meeting)i.next();
Calendar date = Calendar.getInstance(Locale.US);
date.setTime(meeting.getMeetingDate());
switch (date.get(Calendar.DAY_OF_WEEK)) {
case Calendar.MONDAY : daysCode |= Constants.DAY_CODES[Constants.DAY_MON]; break;
case Calendar.TUESDAY : daysCode |= Constants.DAY_CODES[Constants.DAY_TUE]; break;
case Calendar.WEDNESDAY : daysCode |= Constants.DAY_CODES[Constants.DAY_WED]; break;
case Calendar.THURSDAY : daysCode |= Constants.DAY_CODES[Constants.DAY_THU]; break;
case Calendar.FRIDAY : daysCode |= Constants.DAY_CODES[Constants.DAY_FRI]; break;
case Calendar.SATURDAY : daysCode |= Constants.DAY_CODES[Constants.DAY_SAT]; break;
case Calendar.SUNDAY : daysCode |= Constants.DAY_CODES[Constants.DAY_SUN]; break;
}
}
return daysCode;
}
示例2: getDayBetween
import java.util.Calendar; //导入方法依赖的package包/类
/**
* 间隔天数
*
* @param startDate
* @param endDate
* @return
*/
public static final Integer getDayBetween(Date startDate, Date endDate) {
Calendar start = Calendar.getInstance();
start.setTime(startDate);
start.set(Calendar.HOUR_OF_DAY, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
end.set(Calendar.HOUR_OF_DAY, 0);
end.set(Calendar.MINUTE, 0);
end.set(Calendar.SECOND, 0);
end.set(Calendar.MILLISECOND, 0);
long n = end.getTimeInMillis() - start.getTimeInMillis();
return (int) (n / (60 * 60 * 24 * 1000l));
}
示例3: getDay
import java.util.Calendar; //导入方法依赖的package包/类
/**
* 年月日
* @param date
* @return
*/
public static String getDay(Date date) {
TimeZone t = TimeZone.getTimeZone("GMT+08:00");// 获取东8区TimeZone
Calendar calendar = Calendar.getInstance(t);
if (date == null) {
calendar.setTimeInMillis(System.currentTimeMillis());
} else {
calendar.setTime(date);
}
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
String time = year + "-" + (month < 10 ? "0" : "") + month + '-' + (day < 10 ? "0" : "") + day;
return time;
}
示例4: addTimeToDate
import java.util.Calendar; //导入方法依赖的package包/类
private Date addTimeToDate(int mins){
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, mins);
return calendar.getTime();
}
示例5: main
import java.util.Calendar; //导入方法依赖的package包/类
public static void main(String[] args) throws ParseException
{
Map map=new HashMap<>() ;
Date beginDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-01-04 00:00:01");
for (int i = 0; i < 60*60*24*10; i++)
{
Calendar cal = Calendar.getInstance();
cal.setTime(beginDate);
cal.add(Calendar.SECOND, 1);
beginDate = cal.getTime();
int hash = hash(beginDate.getTime(), 3);
if(map.containsKey(hash))
{
map.put(hash, (int)map.get(hash)+1);
} else
{
map.put(hash,1);
}
// System.out.println(hash);
}
System.out.println(map.values());
}
示例6: calculateExpiryDate
import java.util.Calendar; //导入方法依赖的package包/类
public EmailVerification calculateExpiryDate() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Timestamp(cal.getTime().getTime()));
cal.add(Calendar.MINUTE, EXPIRATION);
this.expiryDate = new Date(cal.getTime().getTime());
return this;
}
示例7: getYearFromDate
import java.util.Calendar; //导入方法依赖的package包/类
public static int getYearFromDate(Date date) {
int year = 1990;
if (date != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
year = calendar.get(Calendar.YEAR);
calendar.setTime(date);
year = calendar.get(Calendar.YEAR);
}
return year;
}
示例8: getYearStart
import java.util.Calendar; //导入方法依赖的package包/类
/**
* 得到当前年起始时间
*
* @param date
* @return
*/
public static Date getYearStart(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
示例9: runBaseQuery
import java.util.Calendar; //导入方法依赖的package包/类
private HashMap<String, Long> runBaseQuery(Date date){
Calendar cal = Calendar.getInstance();
long DIFF, TIME = System.currentTimeMillis(), START_TIME = System.currentTimeMillis();
Query baseQuery = em.createQuery(BASE_QUERY);
HashMap<String, Long> result = new HashMap<String, Long>();
{
cal.setTime(date);
int dayMin = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
int dayMax = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, dayMin);
Parameter<Date> p1 = baseQuery.getParameter("oldStartDate", Date.class);
baseQuery.setParameter(p1, cal.getTime());
cal.set(Calendar.DAY_OF_MONTH, dayMax);
Parameter<Date> p2 = baseQuery.getParameter("oldEndDate", Date.class);
baseQuery.setParameter(p2, cal.getTime());
List<Object[]> resultList = baseQuery.getResultList();
DIFF = (System.currentTimeMillis() - TIME);
System.out.println(" Q TIME = "+DIFF+"ms");
for (int i=0; i < resultList.size(); i++){
Object o[] = resultList.get(i);
result.put((String)o[1],(Long)o[0]);
}
}
return result;
}
示例10: hourAndMinuteFromDate
import java.util.Calendar; //导入方法依赖的package包/类
/**
* Create a TimeOfDay from the given date (at the zero-second), in the system default TimeZone.
*
* @param dateTime The java.util.Date from which to extract Hour and Minute.
* @param tz The TimeZone from which relate Hour and Minute for the given date. If null, system default
* TimeZone will be used.
*/
public static TimeOfDay hourAndMinuteFromDate(Date dateTime, TimeZone tz) {
if (dateTime == null)
return null;
Calendar cal = Calendar.getInstance();
cal.setTime(dateTime);
if(tz != null)
cal.setTimeZone(tz);
return new TimeOfDay(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
}
示例11: isToday
import java.util.Calendar; //导入方法依赖的package包/类
public static boolean isToday(Date date) {
Calendar dt1 = getCalendar();
Calendar dt2 = getCalendar();
dt2.setTime(date);
if ((dt1.get(Calendar.MONTH) == dt2.get(Calendar.MONTH)) && (dt1.get(Calendar.DAY_OF_MONTH) == dt2.get(Calendar.DAY_OF_MONTH))) {
return true;
}
return false;
}
示例12: reserve
import java.util.Calendar; //导入方法依赖的package包/类
@Override
public Observable<String> reserve(ReserveOption option) {
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy HHmm");
String startDate = dateFormat.format(option.getDatetime());
Calendar calendar = Calendar.getInstance();
calendar.setTime(option.getDatetime());
calendar.add(Calendar.HOUR,option.getDuration());
String endDate = dateFormat.format(calendar.getTime());
String duration = String.valueOf(option.getDuration());
return sessionRepo.getSession().flatMap( s ->
remoteSource.reserve(option.getCode(),option.getName(),startDate,endDate,duration,s.getUserCode(),s.getToken()));
}
示例13: main3
import java.util.Calendar; //导入方法依赖的package包/类
public static void main3(String[] args) throws ParseException {
Date startDateTime = DateUtil.parserToSecond("2013-05-13 14:02:42");
Date endDateTime = DateUtil.parserToSecond("2013-05-17 12:02:42");
String startHHmmStr = DateUtil.formatToHHmm(startDateTime);
String startDayStr = DateUtil.formatToDay(startDateTime);
String endHHmmStr = DateUtil.formatToHHmm(endDateTime);
String endDayStr = DateUtil.formatToDay(endDateTime);
// 得到二个日期间的间隔天数
long day = (endDateTime.getTime() - startDateTime.getTime())
/ (24 * 60 * 60 * 1000);
// 开始那天
String tStart = startDayStr + "==" + startHHmmStr + "-"
+ DateUtil.formatToHHmm(DateUtil.getCurrEnd(startDateTime));
System.out.println(tStart);
// 中间那些天
for (int i = 0; i < day; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDateTime);
calendar.roll(Calendar.DATE, i + 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sdf.format(calendar.getTime()));
String tMiddle = DateUtil.formatToDay(date) + "=="
+ DateUtil.formatToHHmm(DateUtil.getCurrStart(date)) + "-"
+ DateUtil.formatToHHmm(DateUtil.getCurrEnd(date));
System.out.println(i + 1 + "--" + tMiddle);
}
// 结束那天
String tEnd = endDayStr + "=="
+ DateUtil.formatToHHmm(DateUtil.getCurrStart(endDateTime))
+ "-" + endHHmmStr;
System.out.println(tEnd);
}
示例14: next
import java.util.Calendar; //导入方法依赖的package包/类
/**
* Get the next {@link Date} in the sequence matching the Cron pattern and
* after the value provided. The return value will have a whole number of
* seconds, and will be after the input value.
* @param date a seed value
* @return the next value matching the pattern
*/
public Date next(Date date) {
/*
The plan:
1 Round up to the next whole second
2 If seconds match move on, otherwise find the next match:
2.1 If next match is in the next minute then roll forwards
3 If minute matches move on, otherwise find the next match
3.1 If next match is in the next hour then roll forwards
3.2 Reset the seconds and go to 2
4 If hour matches move on, otherwise find the next match
4.1 If next match is in the next day then roll forwards,
4.2 Reset the minutes and seconds and go to 2
...
*/
Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(this.timeZone);
calendar.setTime(date);
// First, just reset the milliseconds and try to calculate from there...
calendar.set(Calendar.MILLISECOND, 0);
long originalTimestamp = calendar.getTimeInMillis();
doNext(calendar, calendar.get(Calendar.YEAR));
if (calendar.getTimeInMillis() == originalTimestamp) {
// We arrived at the original timestamp - round up to the next whole second and try again...
calendar.add(Calendar.SECOND, 1);
doNext(calendar, calendar.get(Calendar.YEAR));
}
return calendar.getTime();
}
示例15: test_kr_2
import java.util.Calendar; //导入方法依赖的package包/类
public void test_kr_2() throws Exception {
Entity vo = JSON.parseObject("{\"date\":\"2016년5월06일\"}", Entity.class);
Calendar calendar = Calendar.getInstance(JSON.defaultTimeZone, JSON.defaultLocale);
calendar.setTime(vo.date);
Assert.assertEquals(2016, calendar.get(Calendar.YEAR));
Assert.assertEquals(4, calendar.get(Calendar.MONTH));
Assert.assertEquals(6, calendar.get(Calendar.DAY_OF_MONTH));
Assert.assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
Assert.assertEquals(0, calendar.get(Calendar.MINUTE));
Assert.assertEquals(0, calendar.get(Calendar.SECOND));
Assert.assertEquals(0, calendar.get(Calendar.MILLISECOND));
}