java.util.Calendar.getInstance()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(240)

本文整理了Java中java.util.Calendar.getInstance()方法的一些代码示例,展示了Calendar.getInstance()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Calendar.getInstance()方法的具体详情如下:
包路径:java.util.Calendar
类名称:Calendar
方法名:getInstance

Calendar.getInstance介绍

[英]Constructs a new instance of the Calendar subclass appropriate for the default Locale and default TimeZone, set to the current date and time.
[中]构造适用于默认区域设置和默认时区的日历子类的新实例,设置为当前日期和时间。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
  public Calendar convert(Date source) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(source);
    return calendar;
  }
}

代码示例来源:origin: stackoverflow.com

import java.util.Calendar

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);

代码示例来源:origin: stackoverflow.com

String dt = "2008-01-01";  // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);  // number of days to add
dt = sdf.format(c.getTime());  // dt is now the new date

代码示例来源:origin: spring-projects/spring-framework

private Date getDate() {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, 10);
  cal.set(Calendar.MONTH, 10);
  cal.set(Calendar.DATE, 10);
  cal.set(Calendar.HOUR, 10);
  cal.set(Calendar.MINUTE, 10);
  cal.set(Calendar.SECOND, 10);
  return cal.getTime();
}

代码示例来源:origin: stackoverflow.com

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
         cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);

代码示例来源:origin: stanfordnlp/CoreNLP

/** helper for return current date **/
public String currentDate() {
 return new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getTimeFormat_returnsATimeFormat() {
 Calendar cal = Calendar.getInstance();
 cal.clear();
 cal.set(Calendar.HOUR, 7);
 cal.set(Calendar.MINUTE, 48);
 cal.set(Calendar.SECOND, 3);
 Date date = cal.getTime();
 assertThat(getTimeFormat(context).format(date)).isEqualTo("07:48:03");
}

代码示例来源:origin: apache/rocketmq

public static long computNextMinutesTimeMillis() {
  Calendar cal = Calendar.getInstance();
  cal.setTimeInMillis(System.currentTimeMillis());
  cal.add(Calendar.DAY_OF_MONTH, 0);
  cal.add(Calendar.HOUR_OF_DAY, 0);
  cal.add(Calendar.MINUTE, 1);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
  return cal.getTimeInMillis();
}

代码示例来源:origin: org.testng/testng

@Override
public boolean isSkip() {
 if (null == m_expireDate) {
  return false;
 }
 try {
  Calendar now= Calendar.getInstance();
  Date nowDate= m_inFormat.parse(m_inFormat.format(now.getTime()));
  now.setTime(nowDate);
  return !now.after(m_expireDate);
 }
 catch(ParseException pex) {
  throw new TestNGException("Cannot compare dates.");
 }
}

代码示例来源:origin: ctripcorp/apollo

/**
 * Currently the instance config is expired by 1 day, add one more hour to avoid possible time
 * difference
 */
private Date getValidInstanceConfigDate() {
 Calendar cal = Calendar.getInstance();
 cal.add(Calendar.DATE, -1);
 cal.add(Calendar.HOUR, -1);
 return cal.getTime();
}

代码示例来源:origin: spring-projects/spring-framework

private Date getDate(int year, int month, int dayOfMonth, int hour, int minute, int second, int millisecond) {
  Calendar cal = Calendar.getInstance(Locale.US);
  cal.setTimeZone(UTC);
  cal.clear();
  cal.set(Calendar.YEAR, year);
  cal.set(Calendar.MONTH, month);
  cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  cal.set(Calendar.HOUR, hour);
  cal.set(Calendar.MINUTE, minute);
  cal.set(Calendar.SECOND, second);
  cal.set(Calendar.MILLISECOND, millisecond);
  return cal.getTime();
}

代码示例来源:origin: springside/springside4

private static int getWithMondayFirst(final Date date, int field) {
  Validate.notNull(date, "The date must not be null");
  Calendar cal = Calendar.getInstance();
  cal.setFirstDayOfWeek(Calendar.MONDAY);
  cal.setTime(date);
  return cal.get(field);
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * between 2:00 am to 6:00 am, the time is random.
 *
 * @return
 */
long calculateStartTime() {
  Calendar calendar = Calendar.getInstance();
  long nowMill = calendar.getTimeInMillis();
  calendar.set(Calendar.HOUR_OF_DAY, 0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  calendar.set(Calendar.MILLISECOND, 0);
  long subtract = calendar.getTimeInMillis() + ONE_DAY_IN_MIll - nowMill;
  return subtract + (FOUR_HOURS_IN_MIll / 2) + ThreadLocalRandom.current().nextInt(FOUR_HOURS_IN_MIll);
}

代码示例来源:origin: stackoverflow.com

// Create an instance of SimpleDateFormat used for formatting 
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

// Get the date today using Calendar object.
Date today = Calendar.getInstance().getTime();        
// Using DateFormat format method we can create a string 
// representation of a date with the defined format.
String reportDate = df.format(today);

// Print what date is today!
System.out.println("Report Date: " + reportDate);

代码示例来源:origin: spring-projects/spring-framework

private Date getDate() {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, 10);
  cal.set(Calendar.MONTH, 10);
  cal.set(Calendar.DATE, 10);
  cal.set(Calendar.HOUR, 10);
  cal.set(Calendar.MINUTE, 10);
  cal.set(Calendar.SECOND, 10);
  return cal.getTime();
}

代码示例来源:origin: stackoverflow.com

Date date; // your date
 Calendar cal = Calendar.getInstance();
 cal.setTime(date);
 int year = cal.get(Calendar.YEAR);
 int month = cal.get(Calendar.MONTH);
 int day = cal.get(Calendar.DAY_OF_MONTH);
 // etc.

代码示例来源:origin: org.testng/testng

static String timeAsGmt() {
 SimpleDateFormat sdf = new SimpleDateFormat();
 sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
 sdf.applyPattern("dd MMM yyyy HH:mm:ss z");
 return sdf.format(Calendar.getInstance().getTime());
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getDateFormat_returnsADateFormat_December() {
 Calendar cal = Calendar.getInstance();
 cal.clear();
 cal.set(Calendar.DATE, 31);
 cal.set(Calendar.MONTH, Calendar.DECEMBER);
 cal.set(Calendar.YEAR, 1970);
 Date date = cal.getTime();
 assertThat(getDateFormat(context).format(date)).isEqualTo("Dec-31-1970");
}

代码示例来源:origin: apache/rocketmq

public static long computNextHalfHourTimeMillis() {
  Calendar cal = Calendar.getInstance();
  cal.setTimeInMillis(System.currentTimeMillis());
  cal.add(Calendar.DAY_OF_MONTH, 0);
  cal.add(Calendar.HOUR_OF_DAY, 1);
  cal.set(Calendar.MINUTE, 30);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
  return cal.getTimeInMillis();
}

代码示例来源:origin: stackoverflow.com

int year = Calendar.getInstance().get(Calendar.YEAR);

相关文章