当前位置: 首页>>代码示例>>Java>>正文


Java Calendar类代码示例

本文整理汇总了Java中java.util.Calendar的典型用法代码示例。如果您正苦于以下问题:Java Calendar类的具体用法?Java Calendar怎么用?Java Calendar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Calendar类属于java.util包,在下文中一共展示了Calendar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sameDate

import java.util.Calendar; //导入依赖的package包/类
@Test
public void sameDate() throws Exception {
    String format = "yyyyMMddHHmm";
    Calendar calendar = Calendar.getInstance();
    // 月份从0开始: 2017-04-20 15:50
    calendar.set(2017, 03, 20, 15, 50);
    Date date1 = calendar.getTime();
    assertTrue(DateUtils.sameDate(date1, date1, format));

    // 月份从0开始: 2017-05-20 15:50
    calendar.set(2017, 04, 20, 15, 50);
    Date date2 = calendar.getTime();
    assertFalse(DateUtils.sameDate(date1, date2, format));

    // 月份从0开始: 2017-04-20 18:50
    calendar.set(2017, 03, 20, 18, 50);
    date2 = calendar.getTime();
    assertFalse(DateUtils.sameDate(date1, date2, format));

    format = "yyyyMMdd";
    // 月份从0开始: 2017-04-20 18:50
    calendar.set(2017, 03, 20, 18, 50);
    date2 = calendar.getTime();
    assertTrue(DateUtils.sameDate(date1, date2, format));
}
 
开发者ID:ymqq,项目名称:CommonFramework,代码行数:26,代码来源:DateUtilsTest.java

示例2: getMonth

import java.util.Calendar; //导入依赖的package包/类
public static String getMonth(String date) {
    if (TextUtils.isEmpty(date)) {
        return "";
    }

    Calendar dateTime = Calendar.getInstance();

    try {
        dateTime.setTime(getDate(date));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM", Locale.getDefault());
        return simpleDateFormat.format(dateTime.getTime());

    } catch (ParseException e) {
        e.printStackTrace();
    }

    return "";
}
 
开发者ID:rudsonlive,项目名称:liveomvp,代码行数:19,代码来源:DateUtil.java

示例3: onCreateDialog

import java.util.Calendar; //导入依赖的package包/类
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);


    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
            (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
    c.set(Calendar.HOUR_OF_DAY, 12);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
    return datePickerDialog;
}
 
开发者ID:talCrafts,项目名称:Udhari,代码行数:19,代码来源:DatePickerFragment.java

示例4: createEvent

import java.util.Calendar; //导入依赖的package包/类
@Override
public int createEvent(final Event event) {
    if (event == null) {
        throw new IllegalArgumentException("event cannot be null");
    }
    if (event.getId() != null) {
        throw new IllegalArgumentException("event.getId() must be null when creating a new Message");
    }
    final CalendarUser owner = event.getOwner();
    if (owner == null) {
        throw new IllegalArgumentException("event.getOwner() cannot be null");
    }
    final CalendarUser attendee = event.getAttendee();
    if (attendee == null) {
        throw new IllegalArgumentException("attendee.getOwner() cannot be null");
    }
    final Calendar when = event.getWhen();
    if(when == null) {
        throw new IllegalArgumentException("event.getWhen() cannot be null");
    }
    Event newEvent = repository.save(event);
    return newEvent.getId();
}
 
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:24,代码来源:JpaEventDao.java

示例5: Test4655637

import java.util.Calendar; //导入依赖的package包/类
/**
 * 4655637: Calendar.set() for DAY_OF_WEEK does not return the right value
 *
 * <p>Need to use SimpleDateFormat to test because a call to
 * get(int) changes internal states of a Calendar.
 */
public void Test4655637() {
    Locale locale = Locale.getDefault();
    if (!TestUtils.usesGregorianCalendar(locale)) {
        logln("Skipping this test because locale is " + locale);
        return;
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(1029814211523L));
    cal.set(YEAR, 2001);
    Date t = cal.getTime();
    cal.set(MONTH, JANUARY);
    t = cal.getTime();

    cal.set(DAY_OF_MONTH, 8);
    t = cal.getTime();

    cal.set(DAY_OF_WEEK, MONDAY);
    DateFormat df = new SimpleDateFormat("yyyy/MM/dd", Locale.US);
    String expected = "2001/01/08";
    String s = df.format(cal.getTime());
    if (!expected.equals(s)) {
        errln("expected: " + expected + ", got: " + s);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:CalendarRegression.java

示例6: roundDate

import java.util.Calendar; //导入依赖的package包/类
/**
 * Round Up (24:00) or Down (00:00) a datetime
 *
 * @param dateTimeInMillis the datetime in milliseconds
 * @param decodeType       type of decode. See StaticValues.dateDecodeType...
 */
//old name: decodeDateStr
@SuppressLint("WrongConstant")
@SuppressWarnings("SameParameterValue")
public static long roundDate(long dateTimeInMillis, String decodeType) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(dateTimeInMillis);
    if (decodeType.equals(ConstantValues.DATE_DECODE_TO_ZERO)) {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
    else if (decodeType.equals(ConstantValues.DATE_DECODE_TO_24)) {
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
    }
    return cal.getTimeInMillis();
}
 
开发者ID:mkeresztes,项目名称:AndiCar,代码行数:27,代码来源:Utils.java

示例7: testLuceneDateRangeFunction

import java.util.Calendar; //导入依赖的package包/类
/**
 * Test generation of lucene date ranges
 *
 */
public void testLuceneDateRangeFunction()
{
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 2001);
    cal.set(Calendar.MONTH, 1);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    String isoStartDate = ISO8601DateFormat.format(cal.getTime());
    cal.add(Calendar.DAY_OF_MONTH, 1);
    String isoEndDate = ISO8601DateFormat.format(cal.getTime());
    String template = "${luceneDateRange(\""+isoStartDate+"\", \"P1D\")}";
    FreeMarkerWithLuceneExtensionsModelFactory mf = new FreeMarkerWithLuceneExtensionsModelFactory();
    mf.setServiceRegistry(serviceRegistry);
    String result = serviceRegistry.getTemplateService().processTemplateString("freemarker", template, mf.getModel());
    assertEquals(result, "["+isoStartDate+" TO "+isoEndDate+"]");
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:20,代码来源:FreeMarkerModelLuceneFunctionTest.java

示例8: countDiffDay

import java.util.Calendar; //导入依赖的package包/类
public static Integer countDiffDay(String startDate, String endDate) {
  	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  	Calendar cal = Calendar.getInstance(); 
  	long fTime = 0;
  	long oTime = 0;
      try {
	cal.setTime(sdf.parse(startDate));
       fTime = cal.getTimeInMillis();
       
       cal.setTime(sdf.parse(endDate));    
       oTime = cal.getTimeInMillis();  
} catch (ParseException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}    

      long between_days=(oTime-fTime)/(1000*3600*24); 
  	return Integer.parseInt(String.valueOf(between_days));
  }
 
开发者ID:lpj24,项目名称:jobManage,代码行数:20,代码来源:UtilHelper.java

示例9: hasTime

import java.util.Calendar; //导入依赖的package包/类
/**
 * Determines whether or not a date has any time values.
 * 
 * @param date
 *            The date.
 * @return true iff the date is not null and any of the date's hour, minute,
 *         seconds or millisecond values are greater than zero.
 */
public static boolean hasTime(Date date) {
	if (date == null) {
		return false;
	}
	Calendar c = Calendar.getInstance();
	c.setTime(date);
	if (c.get(Calendar.HOUR_OF_DAY) > 0) {
		return true;
	}
	if (c.get(Calendar.MINUTE) > 0) {
		return true;
	}
	if (c.get(Calendar.SECOND) > 0) {
		return true;
	}
	if (c.get(Calendar.MILLISECOND) > 0) {
		return true;
	}
	return false;
}
 
开发者ID:gregoreesmaa,项目名称:minu-poska-android,代码行数:29,代码来源:DateUtils.java

示例10: getBeijingNowTimeString

import java.util.Calendar; //导入依赖的package包/类
public static String getBeijingNowTimeString(String format) {
    TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai");

    Date date = new Date(currentTimeMillis());
    SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
    formatter.setTimeZone(timezone);

    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.setTimeZone(timezone);
    String prefix = gregorianCalendar.get(Calendar.AM_PM) == Calendar.AM ? "上午" : "下午";

    return prefix + formatter.format(date);
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:14,代码来源:TimeUtil.java

示例11: executeAsyncTaskFour

import java.util.Calendar; //导入依赖的package包/类
/**
 * 检测主机cpu使用状态
 * @param res
 */
@Async
public void executeAsyncTaskFour(ManagerTomcatEntity res){
   try {

       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
       HostCpuResultEntity cpuInfo = new HostCpuResultEntity();
       cpuInfo.setId(UUIDUtil.generate());
       cpuInfo.setTomcatId(res.getId());
       cpuInfo.setIp(res.getIp());
       Calendar cal = Calendar.getInstance(TimeZone.getDefault());
       cpuInfo.setTime(System.currentTimeMillis() / 1000 * 1000 + cal.getTimeZone().getRawOffset());
       cpuInfo.setTimeText(dateFormat.format(new Date()));
       String userName = res.getHostUser();
       String password = res.getHostPassword();
       String ip = res.getIp();
       String result = SSHUtil.getCpuInfo(userName, password, ip);
       if (result == null) {
           cpuInfo.setResult("ssh连接失败!");
       } else if ("错误:该主机下含有多个tomcat应用!".equals(result)) {
           cpuInfo.setResult("错误:该主机下含有多个tomcat应用!");
       } else {
           cpuInfo.setResult("200");
           cpuInfo.setCpuInfo(Double.parseDouble(result));
       }
       res.setHostCpuResultEntity(cpuInfo);
   }catch (Exception e){
       e.printStackTrace();
   }finally {
       res.setCpuStatus(true);
   }
}
 
开发者ID:Martion2017,项目名称:ApplicationDetection,代码行数:36,代码来源:AsyncTaskService.java

示例12: createEventFormAutoPopulate

import java.util.Calendar; //导入依赖的package包/类
/**
 * Populates the form for creating an event with valid information. Useful so that users do not have to think when
 * filling out the form for testing.
 *
 * @param createEventForm
 * @return
 */
@PostMapping(value = "/new", params = "auto")
public String createEventFormAutoPopulate(@ModelAttribute CreateEventForm createEventForm) {
    // provide default values to make user submission easier
    createEventForm.setSummary("A new event....");
    createEventForm.setDescription("This was autopopulated to save time creating a valid event.");
    createEventForm.setWhen(Calendar.getInstance());

    // make the attendee not the current user
    CalendarUser currentUser = userContext.getCurrentUser();
    int attendeeId = currentUser.getId() == 0 ? 1 : 0;
    CalendarUser attendee = calendarService.getUser(attendeeId);
    createEventForm.setAttendeeEmail(attendee.getEmail());

    return "events/create";
}
 
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:23,代码来源:EventsController.java

示例13: extractDateTimeInfo

import java.util.Calendar; //导入依赖的package包/类
/**
 * Retourne un tableau d'entiers avec le jour, le mois, l'année, l'heure,
 * les minutes, les secondes et les milisecondes de la date spécifiée
 * en paramètre.
 *
 * @param date une date de type java.util.Date
 * @return un tableau[7] avec JJ, MM, AA, hh, mm, ss, ms
 */
public static int[] extractDateTimeInfo(Date date) {
  int[] info = new int[] {-1, -1, -1, -1, -1, -1, -1};
  if (date != null) {
    Calendar c = new GregorianCalendar();
    c.setTime(date);
    info[0] = c.get(Calendar.DAY_OF_MONTH);
    info[1] = c.get(Calendar.MONTH) + 1;
    info[2] = c.get(Calendar.YEAR);
    info[3] = c.get(Calendar.HOUR_OF_DAY);
    info[4] = c.get(Calendar.MINUTE);
    info[5] = c.get(Calendar.SECOND);
    info[6] = c.get(Calendar.MILLISECOND);
  }
  return info;
}
 
开发者ID:elgoupil,项目名称:GoupilBot,代码行数:24,代码来源:DateTimeLib.java

示例14: write

import java.util.Calendar; //导入依赖的package包/类
@Override
public void write(JsonWriter out, Calendar value) throws IOException {
  if (value == null) {
    out.nullValue();
    return;
  }
  out.beginObject();
  out.name(YEAR);
  out.value(value.get(Calendar.YEAR));
  out.name(MONTH);
  out.value(value.get(Calendar.MONTH));
  out.name(DAY_OF_MONTH);
  out.value(value.get(Calendar.DAY_OF_MONTH));
  out.name(HOUR_OF_DAY);
  out.value(value.get(Calendar.HOUR_OF_DAY));
  out.name(MINUTE);
  out.value(value.get(Calendar.MINUTE));
  out.name(SECOND);
  out.value(value.get(Calendar.SECOND));
  out.endObject();
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:22,代码来源:TypeAdapters.java

示例15: betweenDurations

import java.util.Calendar; //导入依赖的package包/类
/**
 * 两个日期之间相差的月数
 * @param startDate
 * @param endDate
 * @return
 */
public int betweenDurations(String startDate, String endDate){

    int month = 0;
    int day = 0;

    SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.YYYY_MM_DD.getValue());
    Calendar startCalendar = Calendar.getInstance();
    Calendar endCalendar = Calendar.getInstance();
    try {
        startCalendar.setTime(sdf.parse(startDate));
        endCalendar.setTime(sdf.parse(endDate));

        month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
        day = endCalendar.get(Calendar.DAY_OF_MONTH) - startCalendar.get(Calendar.DAY_OF_MONTH);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("date format exception, start date " + startDate + ", end date " + endDate);
    }

    if(day > 0){
        month = month + 1;
    }

    return month;
}
 
开发者ID:Shmilyz,项目名称:Swap,代码行数:32,代码来源:DateUtil.java


注:本文中的java.util.Calendar类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。