Calendar例項化的方式以及處理時間的工具類
1.Calendar例項化的方式
首先Calendar是一個抽象類,不能直接例項化,Java提供了靜態工廠方法getInstance()來例項化Calendar,
或者通過它的子類來例項化
Calendar c=new GregorianCalendar();//直接建立
Calendar c=Calendar.getInstance();//工廠方法
@通過靜態工廠方法例項化Calendar物件
package com.brc.util;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtil {
// 獲取當天的開始時間
public static Date getDayBegin(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
// 獲取當天的結束時間
public static Date getDayEnd(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
// 獲取本週的開始時間
public static Date getBeginDayOfWeek(Date date) {
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(cal.getTime());
}
// 獲取本週的結束時間
public static Date getEndDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setTime(getBeginDayOfWeek(date));
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
// 獲取本月的開始時間
public static Date getBeginDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(getNowYear(date), getNowMonth(date) - 1, 1);
return getDayStartTime(calendar.getTime());
}
// 獲取本月的結束時間
public static Date getEndDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(getNowYear(date), getNowMonth(date) - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(date), getNowMonth(date) - 1, day);
return getDayEndTime(calendar.getTime());
}
// 獲取本月是哪一月
public static int getNowMonth(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
}
// 獲取今年是哪一年
public static Integer getNowYear(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
// 獲取某個日期的開始時間
public static Date getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
// 獲取某個日期的結束時間
public static Date getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
/**
* 獲取某年某個月開始日期-結束日期
* lin
* @param dayCount
* @return
*/
public static String getBeginDayOfMonth(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate localDate = yearMonth.atDay(1);
LocalDateTime startOfDay = localDate.atStartOfDay();
ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
return dateToStr(Date.from(zonedDateTime.toInstant()));
}
public static String getEndOfMonth(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
return dateToStr(Date.from(zonedDateTime.toInstant()));
}
/**
* 獲取某個日期 後面幾天
* lin
* @param dayCount
* @return
*/
public static Date getDayEndForCount(Integer dayCount) {
Calendar calendar = Calendar.getInstance();
if (0 == dayCount) {
return new Date();
}
calendar.add(Calendar.DATE, dayCount);
return calendar.getTime();
}
/**
* 獲取某個日期 後面幾天
* lin
* @param dayCount
* @return
*/
public static String getStrDayEndForCount(Integer dayCount) {
Calendar calendar = Calendar.getInstance();
if (0==dayCount) {
return dateToStrLong(new Date());
}
calendar.add(Calendar.DATE, dayCount);
return dateToStrLong(calendar.getTime());
}
/**
* 將長時間格式字串轉換為時間 yyyy-MM-dd HH:mm:ss
* lin
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 將長時間格式時間轉換為字串 yyyy-MM-dd HH:mm:ss
* lin
* @param dateDate
* @return
*/
public static String dateToStrLong(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 將短時間格式時間轉換為字串 yyyy-MM-dd
* lin
* @param dateDate
* @param k
* @return
*/
public static String dateToStr(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 將短時間格式時間轉換為字串 yyyy-MM
* lin
* @param dateDate
* @param k
* @return
*/
public static String dateToStrMm(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 將短時間格式字串轉換為時間 yyyy-MM-dd
* lin
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public static String formate(Date date, String parten) {
SimpleDateFormat format = new SimpleDateFormat(parten);
return format.format(date);
}
public static void main(String[] args) {
System.err.println(DateUtil.dateToStrMm(new Date()));
}
/**
*
* @param date
* @return
*/
public static Date setDate(Date date1, Date date2) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
calendar2.set(Calendar.YEAR, calendar1.get(Calendar.YEAR));
calendar2.set(Calendar.MONTH, calendar1.get(Calendar.MONTH));
calendar2.set(Calendar.DAY_OF_MONTH, calendar1.get(Calendar.DAY_OF_MONTH));
return calendar2.getTime();
}
獲取輸入時間的當月的日曆
- publicstaticvoid main(String[] args) throws ParseException {
- String str = "2015-9-22";
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- Date date = df.parse(str); //從字串中解析出日期
- Calendar calendar = new GregorianCalendar(); //例項化一個格林尼治日曆
-
//把Date轉化為Calendar再進行處理
- calendar.setTime(date);
- int today = calendar.get(Calendar.DATE); //獲得日數,注意不要用DATE類,已廢棄
- //把日期設為當月的第一天
- calendar.set(Calendar.DATE, 1);
- int firstDay = calendar.get(Calendar.DAY_OF_WEEK); //獲得某一天是一週的星期幾
-
int lastDay = calendar.getActualMaximum(Calendar.DATE);
- System.out.println("日\t一\t二\t三\t四\t五\t六");
- for(int i = 1; i<firstDay; i++) System.out.print("\t");
- for(int i = 1; i<=lastDay; i++) {
- if(i == today) {
- System.out.print("*");
- }
- System.out.print(i + "\t");
- if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
- System.out.println();
- calendar.add(Calendar.DATE, 1); //日期的日數加1
- }
- }
}