/**
* 计算两个日期之间相差的天数
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int DaysMinus(Date startDate,Date endDate) throws ParseException
{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
startDate = sdf.parse(sdf.format(startDate));
endDate = sdf.parse(sdf.format(endDate));
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
long startTime = cal.getTimeInMillis();
cal.setTime(endDate);
long endTime = cal.getTimeInMillis();
long between_days=(endTime-startTime)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
*字符串的日期格式的计算相差天数
*@param 开始日期:startDate
*@param 结束日期:endDate
*@return 相差天数
*/
public static int DaysMinus(String startDate,String endDate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(startDate));
long startTime = cal.getTimeInMillis();
cal.setTime(sdf.parse(endDate));
long endTime = cal.getTimeInMillis();
long between_days = (endTime-startTime)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* String类型日期转Date
* @param date
* @return
* @throws ParseException
*/
public static Date string2Date(String date) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date resultDate = sdf.parse(date);
return resultDate;
}
/**
* Date类型日期转String
* @param date
* @return
* @throws ParseException
*/
public static String date2String(Date date) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentTime = sdf.format(date);
return currentTime;
}
/**
* 日期相加
* @param date
* @param days
* @return
*/
public static Date dateAdd(Date date, int days) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, days);
date = calendar.getTime();
return date;
}
/**
* String 类型日期相加
* @param date
* @param days
* @return
* @throws ParseException
*/
public static Date dateAdd(String date, int days) throws ParseException {
Date Datedate = string2Date(date);
Calendar calendar = new GregorianCalendar();
calendar.setTime(Datedate);
calendar.add(calendar.DATE, days);
Datedate = calendar.getTime();
return Datedate;
}
/**
* 获取当前日期的周次
* 07:"星期日", 01:"星期一", 02:"星期二", 03:"星期三", 04:"星期四", 05:"星期五", 06:"星期六"
* @author 秦海
* @param dt
* @return
*/
public static String DayOfWeek2MatchDict(Date dt) {
String[] weekDays = {"07", "01", "02", "03", "04", "05", "06"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0){
dayOfWeek = 0;
}
return weekDays[dayOfWeek];
}
/**
* 获取系统当前日期 date类型
* @return
* @throws ParseException
*/
public static Date getDateSysDate() throws ParseException {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String sysDate = sdf.format(now);
Date resultDate = sdf.parse(sysDate);
return resultDate;
}
/**
* 按照传入的格式获取系统date类型世间
* -年-月-日 时:分:秒.毫秒
* {yyyy-MM-dd HH:mm:ss.fff}:使用24小时制格式化日期
* {yyyy-MM-dd hh:mm:ss.fff}:使用12小时制格式化日期
* {yyyy-MM-dd HH:mm:ss zzz}
* {yyyy-MM-dd HH:mm:ss.ff zzz}
* {yyyy-MM-dd HH:mm:ss.fff zzz}
* {yyyy-MM-dd HH:mm:ss.ffff zzz}
* @param format
* @return
* @throws ParseException
*/
public static Date getDateSysDate(String format) throws ParseException {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(format);
String sysDate = sdf.format(now);
Date resultDate = sdf.parse(sysDate);
return resultDate;
}
}
/**
* 根据出生日期获取人的年龄
*
* @param strBirthDate(yyyy-mm-dd or yyyy/mm/dd)
* @return
*/
public static String getPersonAgeByBirthDate(Date dateBirthDate){
if (dateBirthDate == null){
return "";
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strBirthDate=dateFormat.format(dateBirthDate);
//读取当前日期
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DATE);
//计算年龄
int age = year - Integer.parseInt(strBirthDate.substring(0, 4)) - 1;
if (Integer.parseInt(strBirthDate.substring(5,7)) < month) {
age++;
} else if (Integer.parseInt(strBirthDate.substring(5,7))== month && Integer.parseInt(strBirthDate.substring(8,10)) <= day){
age++;
}
return String.valueOf(age);
}
/**
* 根据出生日期获取人的年龄
*
* @param strBirthDate(yyyy-mm-dd or yyyy/mm/dd)
* @return
*/
public static String getPersonAgeByBirthDate(String strBirthDate){
if ("".equals(strBirthDate) || strBirthDate == null){
return "";
}
//读取当前日期
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DATE);
//计算年龄
int age = year - Integer.parseInt(strBirthDate.substring(0, 4)) - 1;
if (Integer.parseInt(strBirthDate.substring(5,7)) < month) {
age++;
} else if (Integer.parseInt(strBirthDate.substring(5,7))== month && Integer.parseInt(strBirthDate.substring(8,10)) <= day){
age++;
}
return String.valueOf(age);
}
/**
* 获取当前日期是星期几<br>
* 0-"星期日", 1-"星期一", 2-"星期二", 3-"星期三", 4-"星期四", 5-"星期五", 6-"星期六"
* @param dt
* @return
*/
public static int getDayOfWeek(Date dt) {
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
return dayOfWeek;
}
/**
* 获取当前日期是星期几
* 0-"星期日", 1-"星期一", 2-"星期二", 3-"星期三", 4-"星期四", 5-"星期五", 6-"星期六"
* @param: strDate
*
*/
public static int getDayOfWeek(String strDate){
String format="yyyy-MM-dd";
//可以方便地修改日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date date = null;
try {
date = dateFormat.parse(strDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
int dayOfWeek=c.get(Calendar.DAY_OF_WEEK)- 1;;
return dayOfWeek;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
/**
* 获取系统当前时间
*/
public static String getSysTime(){
Date now = new Date();
//可以方便地修改日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sysDate = dateFormat.format(now);
return sysDate;
}
/**
* 获取系统当前时间
*/
public static String getSysDate(){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String sysDate = dateFormat.format(now);
return sysDate;
}
/**
* 按格式获取系统当前时间
*
* @param: format
*
*/
public static String getSysDate(String format){
Date now = new Date();
//可以方便地修改日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
String sysDate = dateFormat.format(now);
return sysDate;
}
java中常用的与时间有关的方法(string与date转化,出生日期转年龄,时间计算周次等)
最新推荐文章于 2022-09-21 21:35:50 发布