Hutool日期时间
一.日期时间工具-DateUtil
1.static int age(Date birthday, Date dateToCompare):计算相对于dateToCompare的年龄,常用于计算指定生日在某年的年龄
int age = DateUtil.age(DateUtil.parse("1995-06-08 02:30:00"), DateUtil.parse("2022-07-28 26:30:00"));//输出结果为27
2.static int ageOfNow(Date birthDay):生日转为年龄,计算法定年龄
int age1 = DateUtil.ageOfNow(DateUtil.parse("1995-06-08 02:30:00"));//输出结果为27
3.static int ageOfNow(String birthDay):生日转为年龄,计算法定年龄
int age2 = DateUtil.ageOfNow("1995-06-08 02:30:00");//输出结果为27
4. 在日常解决代码时,遇到了获取当前月即日期的一个问题,从网上从到答案后,有一处问题很不懂,在那个博主获取当前月份时+1;再往后面看,又有一个 获取当日日期-1的代码,经过一番探索后的到了答案,再次记录一下。
MONTH加1的原因
public static final int MONTH 指示月份的 get 和 set 的字段数字。
这是一个特定于日历的值。在格里高利历和罗马儒略历中一年中的第一个月是 JANUARY,它为 0;
最后一个月取决于一年中的月份数。 简单来说,因为这个值的初始值是0,因此我们要用它来表示正确的月份时就需要加1。
WEEK减1的原因
public static final int DAY_OF_WEEK get 和 set 的字段数字,指示一个星期中的某天。
该字段可取的值为 SUNDAY、MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY 和 SATURDAY 观察代码,
你会发现其实week是用来确定随后的字符串week1该如何截取的,我们知道DAY_OF_WEEK是获取当前日期是一周中的第几天,
而一周从周日算起,因此当我们取得这个值之后,为了要正确的截取出随后的字符串week1中的那个中文字符,
因此它需要减1来达到目的。你只要跟踪一下程序运行,观察变量的赋值就会理解它这样编写的用意了。
public static void main(String[] args) {
DateTime parse1 = DateUtil.parse("2022-8-25 02:30:00");
DateTime parse2 = DateUtil.parse("2023-8-24 14:30:50");
DateTime dateTime = DateUtil.beginOfDay(parse1);//获取某天的开始时间
DateTime dateTime1 = DateUtil.beginOfHour(parse1);//获取某小时的开始时间
DateTime dateTime2 = DateUtil.beginOfMinute(parse1);//获取某分钟的开始时间
DateTime dateTime3 = DateUtil.beginOfMonth(parse1);//获取某月的开始时间
DateTime dateTime4 = DateUtil.beginOfQuarter(parse1);//获取某季度的开始时间
DateTime dateTime5 = DateUtil.beginOfSecond(parse1);//获取秒级别的开始时间,即毫秒部分设置为0
DateTime dateTime6 = DateUtil.beginOfWeek(parse1);//获取某周的开始时间,周一定为一周的开始时间
DateTime dateTime7 = DateUtil.beginOfWeek(parse1, true);//获取某周的开始时间,周一为一周的开始时间
DateTime dateTime8 = DateUtil.beginOfWeek(parse1, false);//获取某周的开始时间,周日为一周的开始时间
DateTime dateTime9 = DateUtil.beginOfYear(parse1);//获取某年的开始时间
long between = DateUtil.between(parse1, parse2, DateUnit.DAY);//判断两个日期相差的时长,只保留绝对值,unit - 相差的单位:相差 天DateUnit.DAY、小时DateUnit.HOUR 等
long between1 = DateUtil.between(parse1, parse2, DateUnit.DAY, false);//判断两个日期相差的时长
long between2 = DateUtil.betweenDay(parse1, parse2, false);//判断两个日期相差的天数
long between3 = DateUtil.betweenMonth(parse1, parse2, false);//计算两个日期相差月数,在非重置情况下,如果起始日期的天大于结束日期的天,月数要少算1(不足1个月)
long between4 = DateUtil.betweenMs(parse1, parse2);//判断两个日期相差的毫秒数
long between5 = DateUtil.betweenWeek(parse1, parse2, false);//计算指定时间区间内的周数
long between6 = DateUtil.betweenYear(parse1, parse2, false);//计算两个日期相差年数,在非重置情况下,如果起始日期的月大于结束日期的月,年数要少算1(不足1年)
DateTime ceiling = DateUtil.ceiling(parse1, DateField.DAY_OF_MONTH);//修改日期为某个时间字段结束时间
int compare = DateUtil.compare(parse1, parse2);//null安全的日期比较,null对象排在末尾
StopWatch stopWatch = DateUtil.createStopWatch();//创建秒表StopWatch,用于对代码块的执行时间计数
long current = DateUtil.current();//当前时间的时间戳
long currentSecond = DateUtil.currentSeconds();//当前时间的时间戳(秒)
DateTime date = DateUtil.date();//当前时间,转换为DateTime对象
DateTime date1 = DateUtil.date(Calendar.getInstance());//Calendar类型时间转为DateTime
DateTime date2 = DateUtil.date(new Date());//Date类型时间转为DateTime
DateTime date3 = DateUtil.date(1659001318084L);//Long类型时间转为DateTime,只支持毫秒级别时间戳,如果需要秒级别时间戳,请自行×1000
DateTime dateTime10 = DateUtil.dateNew(new Date());//根据已有Date 产生新的DateTime对象
DateTime dateTime11 = DateUtil.dateSecond();//当前时间,转换为DateTime对象,忽略毫秒部分
int i = DateUtil.dayOfMonth(parse1);//获得指定日期是这个日期所在月份的第几天
int i1 = DateUtil.dayOfWeek(parse1);//获得指定日期是星期几,1表示周日,2表示周一
Week week = DateUtil.dayOfWeekEnum(parse1);//获得指定日期是星期几
int i2 = DateUtil.dayOfYear(parse1);//获得指定日期是这个日期所在年的第几天
DateTime dateTime12 = DateUtil.endOfDay(parse1);//获取某天的结束时间
DateTime dateTime13 = DateUtil.endOfHour(parse1);//获取某小时的结束时间
DateTime dateTime14 = DateUtil.endOfMinute(parse1);//获取某分钟的结束时间
DateTime dateTime15 = DateUtil.endOfMonth(parse1);//获取某月的结束时间
DateTime dateTime16 = DateUtil.endOfQuarter(parse1);//获取某季度的结束时间
DateTime dateTime17 = DateUtil.endOfSecond(parse1);//获取秒级别的结束时间,即毫秒设置为999
DateTime dateTime18 = DateUtil.endOfWeek(parse1);//获取某周的结束时间,周日定为一周的结束
DateTime dateTime19 = DateUtil.endOfWeek(parse1, true);//获取某周的结束时间
DateTime dateTime20 = DateUtil.endOfYear(parse1);//获取某年的结束时间
String format = DateUtil.format(parse1, "yyyy-MM-dd HH:mm:ss");//根据特定格式格式化日期
String s4 = DateUtil.formatDate(parse1);//格式化日期部分
String s5 = DateUtil.formatDateTime(parse1);//格式化日期时间,格式 yyyy-MM-dd HH:mm:ss
String s6 = DateUtil.formatTime(parse1);//格式化时间,格式 HH:mm:ss
String s = DateUtil.formatBetween(parse1, parse2);//格式化日期间隔输出,精确到毫秒
String s1 = DateUtil.formatBetween(parse1, parse2, BetweenFormatter.Level.HOUR);//格式化日期间隔输出
String s2 = DateUtil.formatBetween(1659001318084L);//格式化日期间隔输出,精确到毫秒
String s3 = DateUtil.formatBetween(1659001318084L, BetweenFormatter.Level.HOUR);//格式化日期间隔输出
String chineseZodiac = DateUtil.getChineseZodiac(1995);//计算生肖,只计算1900年后出生的人
int hour = DateUtil.hour(parse1, true);//获得指定日期的小时数部分
boolean in = DateUtil.isIn(DateUtil.date(), parse1, parse2);//当前日期是否在日期指定范围内
boolean leapYear = DateUtil.isLeapYear(1995);//是否闰年
boolean sameDay = DateUtil.isSameDay(parse1, parse2);//比较两个日期是否为同一天
boolean sameMonth = DateUtil.isSameMonth(parse1, parse2);//比较两个日期是否为同一月
boolean sameTime = DateUtil.isSameTime(parse1, parse2);//是否为相同时间
DateTime dateTime21 = DateUtil.lastMonth();//上个月
DateTime dateTime22 = DateUtil.lastWeek();//上周
int i3 = DateUtil.lengthOfMonth(6, false);//获得指定月份的总天数
int i4 = DateUtil.lengthOfYear(2022);//获得指定年份的总天数
int millisecond = DateUtil.millisecond(parse1);//获得指定日期的毫秒数部分
int minute = DateUtil.minute(parse1);//获得指定日期的分钟数部分
int month = DateUtil.month(parse1);//获得月份,从0开始计数
Month month1 = DateUtil.monthEnum(parse1);//获得月份
DateTime dateTime23 = DateUtil.nextMonth();//下个月
DateTime dateTime24 = DateUtil.nextWeek();//下周
String now = DateUtil.now();//当前时间,格式 yyyy-MM-dd HH:mm:ss
DateTime offset = DateUtil.offset(parse1, DateField.MONTH, 1);//获取指定日期偏移指定时间后的时间,生成的偏移日期不影响原日期
DateTime dateTime25 = DateUtil.offsetDay(parse1, 1);//偏移天
DateTime dateTime26 = DateUtil.offsetHour(parse1, 1);//偏移小时
DateTime dateTime27 = DateUtil.offsetMillisecond(parse1, 1);//偏移毫秒数
DateTime dateTime28 = DateUtil.offsetMinute(parse1, 1);//偏移分钟
DateTime dateTime29 = DateUtil.offsetMonth(parse1, 1);//偏移月
DateTime dateTime30 = DateUtil.offsetSecond(parse1, 1);//偏移秒数
DateTime dateTime31 = DateUtil.offsetWeek(parse1, 1);//偏移周
DateTime parse3 = DateUtil.parse("2022-07-28 18:42:20");//将日期字符串转换为DateTime对象,格式:yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss yyyy.MM.dd HH:mm:ss yyyy年MM月dd日 HH时mm分ss秒 yyyy-MM-dd yyyy/MM/dd yyyy.MM.dd HH:mm:ss HH时mm分ss秒 yyyy-MM-dd HH:mm yyyy-MM-dd HH:mm:ss.SSS yyyy-MM-dd HH:mm:ss.SSSSSS yyyyMMddHHmmss yyyyMMddHHmmssSSS yyyyMMdd EEE, dd MMM yyyy HH:mm:ss z EEE MMM dd HH:mm:ss zzz yyyy yyyy-MM-dd'T'HH:mm:ss'Z' yyyy-MM-dd'T'HH:mm:ss.SSS'Z' yyyy-MM-dd'T'HH:mm:ssZ yyyy-MM-dd'T'HH:mm:ss.SSSZ
DateTime parse4 = DateUtil.parse("2022-07-28 18:42:20", "yyyy-MM-dd");//将特定格式的日期转换为Date对象
int second = DateUtil.second(parse1);//获得指定日期的秒数部分
String s7 = DateUtil.secondToTime(3600);//秒数转为时间格式(HH:mm:ss)
int i5 = DateUtil.timeToSecond("18:48:20");//HH:mm:ss 时间格式字符串转为秒数
String today = DateUtil.today();//当前日期,格式 yyyy-MM-dd
int i6 = DateUtil.weekOfMonth(parse1);//获得指定日期是所在月份的第几周
int i7 = DateUtil.weekOfYear(parse1);//获得指定日期是所在年份的第几周
int year = DateUtil.year(parse1);//获得年的部分
DateTime yesterday = DateUtil.yesterday();//昨天
DateTime dateTime = new DateTime("2017-9-14 21:55:18", "yyyy-MM-dd HH:mm:ss");
// 获取日期成员(年、季度、月、日)
int year = dateTime.year();// 年
System.out.println("year = " + year);
int season = dateTime.season();// 季度
System.out.println("season = " + season);
int month = dateTime.month();// 月份 Java中MONTH需要加1的
System.out.println("month = " + month);
int dayOfWeek = dateTime.dayOfWeek();// 周几 Java中WEEK减1
System.out.println("dayOfWeek = " + dayOfWeek);
int dayOfMonth = dateTime.dayOfMonth();// 此月第N天
}
5.日期时间对象-DateTime
public static void main(String[] args) {
DateTime parse1 = DateUtil.parse("2022-8-25 02:30:00");
DateTime parse2 = DateUtil.parse("2023-8-24 14:30:50");
DateTime dateTime = new DateTime("2022-07-29 08:30:00");
long between = dateTime.between(DateUtil.date(), DateUnit.MINUTE);//计算相差时长
int i = dateTime.dayOfMonth();//获得指定日期是这个日期所在月份的第几天,从1开始
int i1 = dateTime.dayOfWeek();//获得指定日期是星期几,1表示周日,2表示周一
Week week = dateTime.dayOfWeekEnum();//获得指定日期是星期几
int i2 = dateTime.dayOfWeekInMonth();//获得天所在的周是这个月的第几周
int i3 = dateTime.dayOfYear();//获得指定日期是这个日期所在年份的第几天,从1开始
int field = dateTime.getField(DateField.YEAR);//获得日期的某个部分
int field1 = dateTime.getField(Calendar.YEAR);//获得日期的某个部分
Week firstDayOfWeek = dateTime.getFirstDayOfWeek();//获得一周的第一天,默认为周一
TimeZone timeZone = dateTime.getTimeZone();//获取时区
ZoneId zoneId = dateTime.getZoneId();//获取时区ID
int hour = dateTime.hour(true);//获得指定日期的小时数部分
boolean after = dateTime.isAfter(DateUtil.date());//是否在给定日期之后
boolean afterOrEquals = dateTime.isAfterOrEquals(DateUtil.date());//是否在给定日期之后或与给定日期相等
boolean am = dateTime.isAM();//是否为上午
boolean before = dateTime.isBefore(DateUtil.date());//是否在给定日期之前
boolean beforeOrEquals = dateTime.isBeforeOrEquals(DateUtil.date());//是否在给定日期之前或与给定日期相等
boolean in = dateTime.isIn(parse1, parse2);//当前日期是否在日期指定范围内
boolean leapYear = dateTime.isLeapYear();//是否闰年
boolean pm = dateTime.isPM();//是否为下午
boolean weekend = dateTime.isWeekend();//是否为周末,周末指周六或者周日
int millisecond = dateTime.millisecond();//获得指定日期的毫秒数部分
int minute = dateTime.minute();//获得指定日期的分钟数部分
int month = dateTime.month();//获得月份,从0开始计数
int i4 = dateTime.monthBaseOne();//获取月,从1开始计数
Month month1 = dateTime.monthEnum();//获得月份
int i5 = dateTime.monthStartFromOne();//获得月份,从1开始计数
int quarter = dateTime.quarter();//获得当前日期所属季度,从1开始计数
Quarter quarter1 = dateTime.quarterEnum();//获得当前日期所属季度
int second = dateTime.second();//获得指定日期的秒数部分
String s = dateTime.toDateStr();//转为"yyyy-MM-dd" 格式字符串
String s1 = dateTime.toString();//转为字符串
String s2 = dateTime.toString("yyyy-MM-dd");//转为字符串
String s3 = dateTime.toTimeStr();//转为"HH:mm:ss" 格式字符串
int i6 = dateTime.weekOfMonth();//获得指定日期是所在月份的第几周
int i7 = dateTime.weekOfYear();//获得指定日期是所在年份的第几周
int year = dateTime.year();//获得年的部分
}
普通封装
package com.bq.jframe.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import com.bq.components.exutils.LabelValueBean;
/**
* @作者 UAreMyEyes
* @创建时间 2012-10-10
* @说明
*/
public class DateTimeUtil {
private DateTimeUtil(){}
public static String formatDateTime(String ymd) {
// 格式化当前时间
java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(ymd);
String now = isNow.format(new java.util.Date());
return now;
}
public static String formatDateTime(String ymd, String datetime) {
// 格式化当前时间
java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(ymd);
String now = "";
try {
now = isNow.format(datetime);
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 取得当前年
*
* @return 2007
*/
public static String getTodayYear() {
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy");
return simpledateformat.format(new java.util.Date());
}
/**
* 取得当前月
*
* @return 04
*/
public static String getTodayMonth() {
SimpleDateFormat simpledateformat = new SimpleDateFormat("MM");
return simpledateformat.format(new java.util.Date());
}
/**
* 取得当前天
*
* @return 04
*/
public static String getTodayDay() {
SimpleDateFormat simpledateformat = new SimpleDateFormat("dd");
return simpledateformat.format(new java.util.Date());
}
/**
* 取得当前年月
*
* @return 2004-04
*/
public static String getTodayYearMonth() {
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM");
return simpledateformat.format(new java.util.Date());
}
/**
* 取得今天日期
*
* @return 2007-04-04
*/
public static String getToday() {
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
return simpledateformat.format(new java.util.Date());
}
/**
* 取得今天完整 日期时间
*
* @return 2007-04-04 12:12:12
*/
public static String getTodayDateTime() {
SimpleDateFormat simpledateformat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return simpledateformat.format(new java.util.Date());
}
/**
* 取得今天完整时间
*
* @return 12:12:12
*/
public static String getTodayTime() {
SimpleDateFormat simpledateformat = new SimpleDateFormat("HH:mm:ss");
return simpledateformat.format(new java.util.Date());
}
/**
* 比较当前日期和指定日期 return boolean 如果当前日期在指定日期之后返回true否则返回flase
*/
public static boolean dateCompare(String str) {
boolean bea = false;
SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
String isDate = sdf_d.format(new java.util.Date());
java.util.Date date1;
java.util.Date date0;
try {
date1 = sdf_d.parse(str);
date0 = sdf_d.parse(isDate);
if (date0.after(date1)) {
bea = true;
}
} catch (ParseException e) {
bea = false;
}
return bea;
}
/*
* 比较当前月份和指定月份 如果当前月份在指定月份之后返回true否则返回flase
*/
public static boolean monthCompare(String str) {
boolean bea = false;
SimpleDateFormat sdf_m = new SimpleDateFormat("yyyy-MM");
String isMonth = sdf_m.format(new java.util.Date());
java.util.Date date1;
java.util.Date date0;
try {
date1 = sdf_m.parse(str);
date0 = sdf_m.parse(isMonth);
if (date0.after(date1)) {
bea = true;
}
} catch (ParseException e) {
bea = false;
}
return bea;
}
/*
* 比较当前日期和指定日期 return boolean 如果当前日期在指定日期之后返回true否则返回flase
*/
public static boolean secondCompare(String str) {
boolean bea = false;
SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String isDate = sdf_d.format(new java.util.Date());
java.util.Date date1;
java.util.Date date0;
try {
date1 = sdf_d.parse(str);
date0 = sdf_d.parse(isDate);
if (date0.after(date1)) {
bea = true;
}
} catch (ParseException e) {
bea = false;
}
return bea;
}
/**
* 比较指定两日期如果str1晚于str2则return true;
*
* @param str1
* @param str2
* @return
*/
public static boolean secondCompare(String str1, String str2) {
boolean bea = false;
SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date1;
java.util.Date date0;
try {
date1 = sdf_d.parse(str1);
date0 = sdf_d.parse(str2);
if (date0.after(date1)) {
bea = true;
}
} catch (ParseException e) {
bea = false;
}
return bea;
}
/**
* 设置间隔数后返回时间
*
* @param type
* 间隔类型 秒或者天
* @param 间隔数字
* 比如1秒或者一天
* @return
*/
public static String dateAdd(String type, int i) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = formatDateTime("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance(); // 当时的日期和时间
if (type.equals("s")) {
int s = c.get(Calendar.SECOND);
s = s + i;
c.set(Calendar.SECOND, s);
str = df.format(c.getTime());
} else if (type.equals("d")) {
int d = c.get(Calendar.DAY_OF_MONTH); // 取出“日”数
d = d + i;
c.set(Calendar.DAY_OF_MONTH, d); // 将“日”数设置回去
str = df.format(c.getTime());
} else if (type.equals("m")) {
int m = c.get(Calendar.MONTH);
m = m + i;
c.set(Calendar.MONTH, m);
str = df.format(c.getTime());
} else if (type.equals("y")) {
int y = c.get(Calendar.YEAR);
y = y + i;
c.set(Calendar.YEAR, y);
str = df.format(c.getTime());
}
return str;
}
/**
* 根据输入的日期,设置间隔数后返回时间
*
* @param date
* 日期(2009-03-03 12:12:12)
* @param type
* 间隔类型 秒或者天(s,d,m,y)
* @param 间隔数字
* 比如1秒或者一天
* @return
*/
public static String dateAdd(String dateStr, String type, int i) {
String str = "";
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(dateStr);
// 当时的日期和时间
Calendar c = Calendar.getInstance();
c.setTime(date);
if (type.equals("s")) {
int s = c.get(Calendar.SECOND);
s = s + i;
c.set(Calendar.SECOND, s);
str = df.format(c.getTime());
} else if (type.equals("d")) {
int d = c.get(Calendar.DAY_OF_MONTH); // 取出“日”数
d = d + i;
c.set(Calendar.DAY_OF_MONTH, d); // 将“日”数设置回去
str = df.format(c.getTime());
} else if (type.equals("m")) {
int m = c.get(Calendar.MONTH);
m = m + i;
c.set(Calendar.MONTH, m);
str = df.format(c.getTime());
} else if (type.equals("y")) {
int y = c.get(Calendar.YEAR);
y = y + i;
c.set(Calendar.YEAR, y);
str = df.format(c.getTime());
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/**
* 年增加
*
* @author HPang
* @param date
* @param i
* @return
*/
public static String yearAdd(String date, int i) {
String year = date.substring(0, 4);
date = date.substring(4, 10);
year = String.valueOf(Integer.parseInt(year) + i);
return year + date;
}
/**
* 年减少
*
* @author HPang
* @param date
* @param i
* @return
*/
public static String yearMinus(String date, int i) {
String year = date.substring(0, 4);
date = date.substring(4, 10);
year = String.valueOf(Integer.parseInt(year) - i);
return year + date;
}
/**
* 计算两个日期之间差几个月
*
* @author HPang
* @param beginDate
* @param endDate
* @return
*/
public static Integer betweenMonths(String beginDate, String endDate) {
Integer months = null;
int beginYear = Integer.parseInt(beginDate.substring(0, 4));
int endYear = Integer.parseInt(endDate.substring(0, 4));
int beginMonth = Integer.parseInt(beginDate.substring(5, 7));
int endMonth = Integer.parseInt(endDate.substring(5, 7));
int beginDay = Integer.parseInt(beginDate.substring(8, 10));
int endDay = Integer.parseInt(endDate.substring(8, 10));
int year = endYear - beginYear;
int month = endMonth - beginMonth;
int day = endDay - beginDay;
if (year == 0) {
if (month == 0) {
if (day == 0)
months = new Integer(0);
else if (day > 0)
months = new Integer(1);
} else if (month > 0)
months = new Integer(month);
} else if (year > 0) {
if (month == 0) {
if (day == 0)
months = new Integer(12 * year);
else if (day > 0)
months = new Integer(12 * year + 1);
} else if (month > 0)
months = new Integer(12 * year + month);
else {
months = new Integer(12 * year + month + 1);
}
}
return months;
}
/**
* 计算两个日期之间差多少天
*
* @author HPang
* @param begin
* @param end
* @return
*/
public static Long betweenDays(String begin, String end) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Long betweenDays = null;
try {
Date beginDate = formatter.parse(begin);
Date endDate = formatter.parse(end);
long beginTime = beginDate.getTime();
long endTime = endDate.getTime();
betweenDays = new Long((long) ((endTime - beginTime)
/ (1000 * 60 * 60 * 24) + 0.5));
return betweenDays;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 返回年度列表(升序)
*
* @author HPang
* @return
*/
@SuppressWarnings("unchecked")
public static List<LabelValueBean> getYears(String startYear) {
int start_year = 1990;
Vector rtnList = new Vector();
Calendar cal = Calendar.getInstance();
int curr_year = cal.get(Calendar.YEAR);
for (int i = start_year; i <= curr_year; i++) {
String year = String.valueOf(i);
rtnList.add(new LabelValueBean(year, year));
}
return rtnList;
}
/**
* 返回指定年度以前的年度列表(升序,包含指定年度)
*
* @author HPang
* @return List<LabelValueBean>
*/
public static List<LabelValueBean> getYears(int curYear) {
int start_year = 1990;
Vector<LabelValueBean> rtnList = new Vector<LabelValueBean>();
for (int i = start_year; i <= curYear; i++) {
String year = String.valueOf(i);
rtnList.add(new LabelValueBean(year, year));
}
return rtnList;
}
/**
* 返回年度列表(降序)
*
* @author HPang
* @return
*/
@SuppressWarnings("unchecked")
public static List<LabelValueBean> getYearsByDesc() {
Vector rtnList = new Vector();
Calendar cal = Calendar.getInstance();
int curr_year = cal.get(Calendar.YEAR);
int show_year = (curr_year - 10);
for (int i = curr_year; i >= show_year; i--) {
String year = String.valueOf(i);
rtnList.add(new LabelValueBean(year, year));
}
return rtnList;
}
/** 返回酒店等级列表 */
public static List<LabelValueBean> getJddj() {
List<LabelValueBean> djList = new ArrayList<LabelValueBean>();
try {
djList.add(new LabelValueBean("一星级", "一星级"));
djList.add(new LabelValueBean("二星级", "二星级"));
djList.add(new LabelValueBean("三星级", "三星级"));
djList.add(new LabelValueBean("四星级", "四星级"));
djList.add(new LabelValueBean("五星级", "五星级"));
return djList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** 返回景区等级列表 */
public static List<LabelValueBean> getJqdj() {
List<LabelValueBean> djList = new ArrayList<LabelValueBean>();
try {
djList.add(new LabelValueBean("1A", "1A"));
djList.add(new LabelValueBean("2A", "2A"));
djList.add(new LabelValueBean("3A", "3A"));
djList.add(new LabelValueBean("4A", "4A"));
return djList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** 返回旅行社等级列表 */
public static List<LabelValueBean> getLxsdj() {
List<LabelValueBean> djList = new ArrayList<LabelValueBean>();
try {
djList.add(new LabelValueBean("一级社", "一级社"));
djList.add(new LabelValueBean("二级社", "二级社"));
djList.add(new LabelValueBean("三级社", "三级社"));
return djList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 返回月份列表
*
* @return
*/
public static List<LabelValueBean> getMonths() {
Vector<LabelValueBean> rtnList = new Vector<LabelValueBean>();
for (int i = 1; i < 13; i++) {
String month = String.valueOf(i);
if (month.length() == 1)
month = "0" + month;
rtnList.add(new LabelValueBean(month, month));
}
return rtnList;
}
/**
* 返回指定月份以前的月份列表。含当前月
*
* @param curMonth
* 当前月份
* @return List<LabelValueBean>
*/
public static List<LabelValueBean> getMonths(int curMonth) {
Vector<LabelValueBean> rtnList = new Vector<LabelValueBean>();
for (int i = 1; i < 13; i++) {
if (i <= curMonth) {
String month = String.valueOf(i);
if (month.length() == 1)
month = "0" + month;
rtnList.add(new LabelValueBean(month, month));
}
}
return rtnList;
}
/**
* 返回日期列表
*
* @return
*/
public static List<LabelValueBean> getDays() {
Vector<LabelValueBean> rtnList = new Vector<LabelValueBean>();
for (int i = 1; i < 31; i++) {
String day = String.valueOf(i);
if (day.length() == 1)
day = "0" + day;
rtnList.add(new LabelValueBean(day, day));
}
return rtnList;
}
/**
* 返回指定月日期列表
*
* @return
*/
public static List<LabelValueBean> getDays(int year, int month) {
Vector<LabelValueBean> rtnList = new Vector<LabelValueBean>();
Calendar cal = Calendar.getInstance();
cal.set(year, month, 1);
int curr_day = cal.get(Calendar.DAY_OF_MONTH);
for (int i = 1; i < curr_day; i++) {
String day = String.valueOf(i);
if (day.length() == 1)
day = "0" + day;
rtnList.add(new LabelValueBean(day, day));
}
return rtnList;
}
/**
* 返回季度列表
*
* @return
*/
public static List<LabelValueBean> getQuarters() {
Vector<LabelValueBean> rtnList = new Vector<LabelValueBean>();
for (int i = 1; i < 5; i++) {
String quarter = String.valueOf(i);
rtnList.add(new LabelValueBean("第" + quarter + "季度", quarter));
}
return rtnList;
}
/**
* 返回半年列表
*
* @return
*/
public static List<LabelValueBean> getHalfYear() {
Vector<LabelValueBean> rtnList = new Vector<LabelValueBean>();
rtnList.add(new LabelValueBean("上半年", "1"));
rtnList.add(new LabelValueBean("下半年", "2"));
return rtnList;
}
/**
* 返回学期列表
*
* @return
*/
public static List<LabelValueBean> getHalfTerm() {
Vector<LabelValueBean> rtnList = new Vector<LabelValueBean>();
rtnList.add(new LabelValueBean("上学期", "1"));
rtnList.add(new LabelValueBean("下学期", "2"));
return rtnList;
}
/**
* 根据输入的年月返回这个月的第一天和最后一天
*
* @param yearMonth
* 格式:2005-09
* @return String[0]:这个月的第一天(2005-09-01);String[1]:这个月的最后一天(2005-09-31)
*/
public static String[] getFirstAndLastDate(String yearMonth) {
String[] date = new String[2];
String[] d = yearMonth.split("-");
if (d.length != 2 || d[0].length() != 4 || d[1].length() != 2) {
date = null;
} else {
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(d[0]), Integer.parseInt(d[1]) - 1, 1);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int firstDay = cal.getActualMinimum(Calendar.DATE);
int lastDay = cal.getActualMaximum(Calendar.DATE);
cal.set(year, month, firstDay, 0, 0, 0);
Date begin = cal.getTime();
cal.set(year, month, lastDay, 23, 59, 59);
Date end = cal.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String b_d = df.format(begin);
String e_d = df.format(end);
date[0] = b_d;
date[1] = e_d;
}
return date;
}
/**
* 根据当前日期,返回一个循环年月,截止到当前年月的一年12个月份
*
* @return String[2008-06,....,2009-05]
*/
public static String[] getCycleOneYearMonth() {
String _todayDateTime = getTodayDateTime();
String[] yearMonths = new String[12];
int n = 0;
for (int i = 12; i > 0; i--) {
String temp = dateAdd(_todayDateTime, "m", -i);
yearMonths[n] = temp.substring(0, 7);
n++;
}
return yearMonths;
}
/**
* 格式化日期
*
* @param date
* @param format
* @return
*/
public static String formatDate(Date date, String format) {
String now = "";
if (date != null) {
// 格式化当前时间
java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(
format);
now = isNow.format(date);
}
return now;
}
}