下面列出了java.util.Calendar#getInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void settingInformation() {
if(getIntent()!=null) {
Calendar calendar = Calendar.getInstance();
String date = String.format("%s, %d/%d", convertToDayOfWeek(calendar.get(Calendar.DAY_OF_WEEK)), calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH));
txtDate.setText(date);
txtFee.setText(String.format("$ %.2f", getIntent().getDoubleExtra("total", 0.0)));
txtEstimatedPayout.setText(String.format("$ %.2f", getIntent().getDoubleExtra("total", 0.0)));
txtBaseFare.setText(String.format("$ %.2f", Common.baseFare));
txtTime.setText(String.format("%s min", getIntent().getStringExtra("time")));
txtDistance.setText(String.format("%s km", getIntent().getStringExtra("distance")));
txtFrom.setText(getIntent().getStringExtra("start_address"));
txtTo.setText(getIntent().getStringExtra("end_address"));
//add icon_marker
String[] location_end = getIntent().getStringExtra("location_end").split(",");
LatLng dropOff = new LatLng(Double.parseDouble(location_end[0]), Double.parseDouble(location_end[1]));
mMap.addMarker(new MarkerOptions().position(dropOff)
.title("Drop Off Here")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_destination_marker)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dropOff, 12.0f));
}
}
public void testSMTP(){
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar cal = Calendar.getInstance(timeZone);
cal.set(2003,5,8,10,11,12);
String text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
text = DateFormatUtils.SMTP_DATETIME_FORMAT.format(cal);
assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
// format UTC
text = DateFormatUtils.formatUTC(cal.getTime().getTime(),
DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
assertEquals("Sun, 08 Jun 2003 13:11:12 +0000", text);
}
private void assertEqualDuration(String message, String expected, int[] start, int[] end, String format) {
Calendar cal1 = Calendar.getInstance();
cal1.set(start[0], start[1], start[2], start[3], start[4], start[5]);
cal1.set(Calendar.MILLISECOND, 0);
Calendar cal2 = Calendar.getInstance();
cal2.set(end[0], end[1], end[2], end[3], end[4], end[5]);
cal2.set(Calendar.MILLISECOND, 0);
long milli1 = cal1.getTime().getTime();
long milli2 = cal2.getTime().getTime();
String result = DurationFormatUtils.formatPeriod(milli1, milli2, format);
if (message == null) {
assertEquals(expected, result);
} else {
assertEquals(message, expected, result);
}
}
@Test
public void shouldReturn10thNovember2010time8hour20minWhenConvertToLocalDateTime() {
// given
Calendar calendar = Calendar.getInstance();
calendar.set(2010, 10, 10, 8, 20);
Date dateToConvert = calendar.getTime();
// when
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTime(dateToConvert);
// then
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
}
/**
*
* 获取两个日期之间的天数
* 例如:startDate=2016-04-03 endDate=2016-04-05
* 返回:2
*
* @author zhangshaobin
* @created 2016年4月3日 下午午17:41:45
*
* @param startDate
* @param endDate
* @return
*/
public static int getDatebetweenOfDayNum(Date startDate, Date endDate ) {
try {
startDate = parseDate(getDateFormat().format(startDate.getTime()), dateFormatPattern);
endDate = parseDate(getDateFormat().format(endDate.getTime()), dateFormatPattern);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
long time1 = cal.getTimeInMillis();
cal.setTime(endDate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 按用户格式字符串距离今天的天数
*
* @param date 日期字符串
* @param format 日期格式
* @return
*/
public static int countDays(String date, String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
public DatePicker(ReadableMap options, Callback callback) {
final Calendar c = Calendar.getInstance();
this.callback = callback;
year = options.hasKey("year") ? options.getInt("year") : c.get(Calendar.YEAR);
month = options.hasKey("month") ? options.getInt("month") : c.get(Calendar.MONTH);
day = options.hasKey("day") ? options.getInt("day") : c.get(Calendar.DAY_OF_MONTH);
}
public static boolean inSameDay(Date date1, Date Date2) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
int year1 = calendar.get(Calendar.YEAR);
int day1 = calendar.get(Calendar.DAY_OF_YEAR);
calendar.setTime(Date2);
int year2 = calendar.get(Calendar.YEAR);
int day2 = calendar.get(Calendar.DAY_OF_YEAR);
if ((year1 == year2) && (day1 == day2)) {
return true;
}
return false;
}
/**
* Creates a time period for the week in which the specified date/time
* falls, calculated relative to the specified time zone.
*
* @param time the date/time (<code>null</code> not permitted).
* @param zone the time zone (<code>null</code> not permitted).
* @param locale the locale (<code>null</code> not permitted).
*
* @since 1.0.7
*/
public Week(Date time, TimeZone zone, Locale locale) {
if (time == null) {
throw new IllegalArgumentException("Null 'time' argument.");
}
if (zone == null) {
throw new IllegalArgumentException("Null 'zone' argument.");
}
if (locale == null) {
throw new IllegalArgumentException("Null 'locale' argument.");
}
Calendar calendar = Calendar.getInstance(zone, locale);
calendar.setTime(time);
// sometimes the last few days of the year are considered to fall in
// the *first* week of the following year. Refer to the Javadocs for
// GregorianCalendar.
int tempWeek = calendar.get(Calendar.WEEK_OF_YEAR);
if (tempWeek == 1
&& calendar.get(Calendar.MONTH) == Calendar.DECEMBER) {
this.week = 1;
this.year = (short) (calendar.get(Calendar.YEAR) + 1);
}
else {
this.week = (byte) Math.min(tempWeek, LAST_WEEK_IN_YEAR);
int yyyy = calendar.get(Calendar.YEAR);
// alternatively, sometimes the first few days of the year are
// considered to fall in the *last* week of the previous year...
if (calendar.get(Calendar.MONTH) == Calendar.JANUARY
&& this.week >= 52) {
yyyy--;
}
this.year = (short) yyyy;
}
peg(calendar);
}
public EventWithoutYear(CalendarEvent baseEvent) {
this.baseEvent = baseEvent;
Calendar currCal = Calendar.getInstance();
currCal.setTime(baseEvent.getDate());
this.baseYear = currCal.get(Calendar.YEAR);
int startYear = baseYear - X_YEAR;
int endYear = baseYear + Y_YEAR;
listYears = new ArrayList<>();
for (int iteratedYear = startYear; iteratedYear <= endYear; iteratedYear++) {
listYears.add(iteratedYear);
}
}
/**
* 获取一年中某周,星期几的日期
*
* @param yearNum 年
* @param weekNum 那一周
* @param dayOfWeek 周几
* @return 日期
*/
private static Date getDateOfYearWeek(int yearNum, int weekNum,
int dayOfWeek) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
cal.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
cal.set(Calendar.YEAR, yearNum);
cal.set(Calendar.WEEK_OF_YEAR, weekNum);
/*
* cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0);
* cal.set(Calendar.SECOND, 0);
*/
return cal.getTime();
}
public static long getTimeDifference(final Date date1, final Date date2) {
final Calendar dateCalendar1 = Calendar.getInstance();
dateCalendar1.setTime(date1);
final Calendar dateCalendar2 = Calendar.getInstance();
dateCalendar2.setTime(date2);
return (dateCalendar1.getTimeInMillis() - dateCalendar2.getTimeInMillis()) / 1000;
}
/**
* Generate Calendar object and return it
* @param timestamp timestamp for which Calendar object to be created
* @return Calendar object
*/
private Calendar toCalendar(long timestamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar;
}
public Date getDate()
{
if (calendar == null) {
calendar = Calendar.getInstance();
}
calendar.set(year, month, day);
return calendar.getTime();
}
/**
* Returns a JDK calendar type for given locale and time zone.
*
* @param tz time zone
* @param l locale
* @return a calendar for given locale and time zone.
*/
public Calendar getCalendar(TimeZone tz, Locale locale) {
/* TODO: uncomment following code once ICU fixes the calendar bugs. Then API versioning.
* http://bugs.icu-project.org/trac/ticket/13548
* http://bugs.icu-project.org/trac/ticket/13601
if (shouldUseICU(locale)) {
// Create ICU time zone instance.
com.ibm.icu.util.TimeZone icu_tz = com.ibm.icu.util.TimeZone.getTimeZone(tz.getID());
// Wrap ICU calendar into Java calendar type.
return CalendarICU.wrap(com.ibm.icu.util.Calendar.getInstance(icu_tz, locale));
}
*/
return Calendar.getInstance(tz, locale);
}
public static String getDisplayDate(Context context, int datetime) {
String[] weekTitle = context.getResources().getStringArray(R.array.weeks);
int year = datetime / 10000;
int month = (datetime % 10000) / 100 - 1;
int day = datetime % 100;
Date date = new GregorianCalendar(year, month, day).getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int week = calendar.get(calendar.DAY_OF_WEEK);
return TimeUtils.convertByFormatter(date, "MM月dd日") + " " + weekTitle[week - 1];
}
TimeAxis(RrdGraph rrdGraph) {
this.rrdGraph = rrdGraph;
if (rrdGraph.im.xsize > 0) {
this.secPerPix = (rrdGraph.im.end - rrdGraph.im.start) / Double.valueOf(rrdGraph.im.xsize);
}
this.calendar = Calendar.getInstance(Locale.getDefault());
this.calendar.setFirstDayOfWeek(rrdGraph.gdef.firstDayOfWeek);
}
/**
* 获得当前日期的上月的日期(MMM yyyy)
*
* @return 日期
*/
public static String preMonth() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
return String.valueOf(dfMmmYyyy.format(cal.getTime()));
}
/**
* See above.
*/
public static double secondInMinute()
{
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.SECOND);
}
/**
* Gets Date for <code> numberOfDays </code> ago.
*/
public static Date getDayBefore(Date date, int numberOfDays) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -numberOfDays);
return cal.getTime();
}