本文整理汇总了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));
}
示例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 "";
}
示例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;
}
示例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();
}
示例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);
}
}
示例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();
}
示例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+"]");
}
示例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));
}
示例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;
}
示例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);
}
示例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);
}
}
示例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";
}
示例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;
}
示例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();
}
示例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;
}