您好,欢迎访问一九零五行业门户网

Java日期工具类的编写方法有哪些

java日期工具类编写将字符串转换为对应日期
date date = simpledateformat.parse(string);
将日期转换为字符串
string string = simpledateformat.format(date);
注意,因为可能定义的格式和实际字符串提供的格式不符合,所以会抛出异常。
将年月日的汉字日期转为 - - 分隔符的日期
public static void main(string[] args) throws parseexception { //统一日期格式 string strval = "2018年05月22日"; date d1 = new simpledateformat("yyyy年mm月dd日").parse(strval); simpledateformat format = new simpledateformat("yyyy-mm-dd"); string time = format.format(d1); system.out.println(time); }
/** * 将日期对象格式化为指定格式的日期字符串 * @param date 传入的日期对象 * @param format 格式 * @return */ public static string formatdate(date date,string format){ string result=""; simpledateformat sdf=new simpledateformat(format); if(date!=null){ result=sdf.format(date); } return result; } /** * 将日期字符串转换成一个日期对象 * @param datestr 日期字符串 * @param format 格式 * @return * @throws parseexception */ public static date formatdate(string datestr,string format) throws parseexception{ simpledateformat sdf=new simpledateformat(format); return sdf.parse(datestr); } public static void main(string[] args) throws parseexception { date date=new date(); system.out.println(formatdate(date,"yyyy-mm-dd")); system.out.println(formatdate(date,"yyyy-mm-dd hh:mm:ss")); system.out.println(formatdate(date,"yyyy年mm月dd日hh时mm分ss秒")); string datastr="1989-11-02 18:01:41"; date date2=formatdate(datastr,"yyyy-mm-dd hh:mm:ss"); system.out.println(formatdate(date2,"yyyy-mm-dd hh:mm:ss")); }
运行输出:
2016-11-02
2016-11-02 18:06:50
2016年11月02日18时06分50秒
1989-11-02 18:01:41
simpledateformat类主要是用作日期类型转换用的,常用的日期格式化
public static void main(string[] args) { //默认输出格式 date date=new date(); system.out.println(date); //日期格式化显示,首先定义格式 simpledateformat sdf1=new simpledateformat("yyyymmdd"); simpledateformat sdf2=new simpledateformat("yyyy-mm-dd"); simpledateformat sdf3=new simpledateformat("yyyy-mm-dd hh:mm:ss"); simpledateformat sdf4=new simpledateformat("yyyy年mm月dd日hh时mm分ss秒"); //将格式应用于日期 system.out.println(sdf1.format(date)); system.out.println(sdf2.format(date)); system.out.println(sdf3.format(date)); system.out.println(sdf4.format(date)); }
javacalendar日历类的时间操作calendar有个day_of_week 可以返回一个星期中的第几天;
这里说下注意点 老外的第一天是从星期日开始的,所以要-1;
import java.util.calendar;public class test { public static void main(string[] args) { string[] weekdays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; calendar calendar=calendar.getinstance(); system.out.println("今天是"+weekdays[calendar.get(calendar.day_of_week)-1]); }}
对日历进行操作
public static void main(string[] args) { //默认输出格式 date now = new date(); simpledateformat format = new simpledateformat("yyyy-mm-dd"); string time = format.format(now); system.out.println(time); calendar calendar = calendar.getinstance(); calendar.settime(now); calendar.add(calendar.month, 1); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); string nowtime = sdf.format(calendar.gettime()); system.out.println(nowtime); calendar.add(calendar.month, -2); string nowtime2 = sdf.format(calendar.gettime()); system.out.println(nowtime2); }
获取时间
赋值时年月日时分秒常用的6个值,注意月份下标从0开始,所以取月份要+1
public static void main(string[] args) { //默认输出格式 date now = new date(); simpledateformat nowsdf = new simpledateformat("yyyy-mm-dd"); system.out.println(nowsdf.format(now)); calendar calendar = calendar.getinstance(); // 赋值时年月日时分秒常用的6个值,注意月份下标从0开始,所以取月份要+1 system.out.println("年:" + calendar.get(calendar.year)); system.out.println("月:" + (calendar.get(calendar.month) + 1)); system.out.println("日:" + calendar.get(calendar.day_of_month)); system.out.println("时:" + calendar.get(calendar.hour_of_day)); system.out.println("分:" + calendar.get(calendar.minute)); system.out.println("秒:" + calendar.get(calendar.second)); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); string nowtime2 = sdf.format(calendar.gettime()); system.out.println(nowtime2); }
设置时间
如果想设置为某个日期,可以一次设置年月日时分秒,由于月份下标从0开始赋值月份要-1
public static void main(string[] args) { //默认输出格式 date now = new date(); simpledateformat nowsdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); system.out.println(nowsdf.format(now)); calendar calendar = calendar.getinstance(); calendar.set(2013, 5, 4, 13, 44, 51); calendar.set(calendar.year, 2014);//年 simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string nowtime = sdf.format(calendar.gettime()); system.out.println(nowtime); calendar.set(calendar.month, 7);//月(月份0代表1月) simpledateformat sdf1 = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string nowtime1 = sdf1.format(calendar.gettime()); system.out.println(nowtime1); calendar.set(calendar.date, 11);//日 calendar.set(calendar.hour_of_day, 15);//时 calendar.set(calendar.minute, 33);//分 calendar.set(calendar.second, 32);//秒 }
时间计算
public static void main(string[] args) { //默认输出格式 date now = new date(); simpledateformat nowsdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); system.out.println(nowsdf.format(now)); calendar calendar = calendar.getinstance(); calendar.settime(now); calendar.add(calendar.year, 1);//年 calendar.add(calendar.month, 1);//月 calendar.add(calendar.date, 1);//日 //calendar.add(calendar.day_of_year, 1);//今年的第 n 天 //calendar.add(calendar.day_of_month, 1); // 本月第 n 天 //calendar.add(calendar.day_of_week, 1);// 本周几 calendar.add(calendar.hour_of_day, -1);//时 calendar.add(calendar.minute, 1);//分 calendar.add(calendar.second, 1);//秒 //calendar.add(calendar.week_of_month, 1);//增加一个礼拜 //calendar.add(calendar.week_of_year,1);//增加一个礼拜 simpledateformat sdf1 = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string nowtime1 = sdf1.format(calendar.gettime()); system.out.println(nowtime1); }
日期的计算
获取本月最小天数与最大天数
public static void main(string[] args) { //默认输出格式 date now = new date(); simpledateformat nowsdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); calendar calendar = calendar.getinstance(); int firstd = calendar.getactualminimum(calendar.day_of_month); int lastd = calendar.getactualmaximum(calendar.day_of_month); system.out.println("获取本月的第一天和最后天:" + firstd +"," + lastd); }
获取本周星期一、上周星期一、这周星期一
public static date gelastweekmonday(date date) { calendar cal = calendar.getinstance(); cal.settime(getthisweekmonday(date)); cal.add(calendar.date, -7); return cal.gettime(); } public static date getthisweekmonday(date date) { calendar cal = calendar.getinstance(); cal.settime(date); // 获得当前日期是一个星期的第几天 int dayweek = cal.get(calendar.day_of_week); if (1 == dayweek) { cal.add(calendar.day_of_month, -1); } // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 cal.setfirstdayofweek(calendar.monday); // 获得当前日期是一个星期的第几天 int day = cal.get(calendar.day_of_week); // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 cal.add(calendar.date, cal.getfirstdayofweek() - day); return cal.gettime(); } public static date getnextweekmonday(date date) { calendar cal = calendar.getinstance(); cal.settime(getthisweekmonday(date)); cal.add(calendar.date, 7); return cal.gettime(); } public static void main(string[] args) { simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); try { date date = sdf.parse("2017-09-10"); system.out.println("今天是" + sdf.format(date)); system.out.println("上周一" + sdf.format(gelastweekmonday(date))); system.out.println("本周一" + sdf.format(getthisweekmonday(date))); system.out.println("下周一" + sdf.format(getnextweekmonday(date))); } catch (exception e) { e.printstacktrace(); } }
天数计算
用date类计算日期差
public static void main(string[] args) { calendar love = calendar.getinstance(); calendar now = calendar.getinstance(); love.set(2016, 8, 6); //真实的日期是2016-9-6; int days = (int) ((now.gettimeinmillis() - love.gettimeinmillis()) / (24 * 60 * 60 * 1000.0)); system.out.println(days); }
public static void main(string[] args) throws parseexception { simpledateformat format=new simpledateformat("yyyy-mm-dd");//大小写还是很重要的 date loveday=new date(); date now=new date(); loveday=format.parse("2016-08-06"); int day=(int) ((now.gettime()-loveday.gettime())/(24*60*60*1000)); system.out.println(day);}
日期工具类 import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date;public class dateutils { public static final string date_time_pattern = "yyyy-mm-dd hh:mm:ss"; public static final string minute_pattern = "yyyy-mm-dd hh:mm"; public static final string hour_pattern = "yyyy-mm-dd hh:mm:ss"; public static final string date_pattern = "yyyy-mm-dd"; public static final string month_pattern = "yyyy-mm"; public static final string year_pattern = "yyyy"; public static final string minute_only_pattern = "mm"; public static final string hour_only_pattern = "hh"; /** * 日期相加减天数 * @param date 如果为null,则为当前时间 * @param days 加减天数 * @param includetime 是否包括时分秒,true表示包含 * @return * @throws parseexception */ public static date dateadd(date date, int days, boolean includetime) throws parseexception{ if(date == null){ date = new date(); } if(!includetime){ simpledateformat sdf = new simpledateformat(dateutils.date_pattern); date = sdf.parse(sdf.format(date)); } calendar cal = calendar.getinstance(); cal.settime(date); cal.add(calendar.date, days); return cal.gettime(); } /** * 时间格式化成字符串 * @param date date * @param pattern strutils.date_time_pattern || strutils.date_pattern, 如果为空,则为yyyy-mm-dd * @return * @throws parseexception */ public static string dateformat(date date, string pattern) throws parseexception{ if(pattern==null||pattern.length()==0||pattern.equals(" ")){ pattern = dateutils.date_pattern; } simpledateformat sdf = new simpledateformat(pattern); return sdf.format(date); } /** * 字符串解析成时间对象 * @param datetimestring string * @param pattern strutils.date_time_pattern || strutils.date_pattern,如果为空,则为yyyy-mm-dd * @return * @throws parseexception */ public static date dateparse(string datetimestring, string pattern) throws parseexception{ if(pattern==null||pattern.length()==0||pattern.equals(" ")){ pattern = dateutils.date_pattern; } simpledateformat sdf = new simpledateformat(pattern); return sdf.parse(datetimestring); } /** * 将日期时间格式成只有日期的字符串(可以直接使用dateformat,pattern为null进行格式化) * @param datetime date * @return * @throws parseexception */ public static string datetimetodatestring(date datetime) throws parseexception{ string datetimestring = dateutils.dateformat(datetime, dateutils.date_time_pattern); return datetimestring.substring(0, 10); } /** * 当时、分、秒为00:00:00时,将日期时间格式成只有日期的字符串, * 当时、分、秒不为00:00:00时,直接返回 * @param datetime date * @return * @throws parseexception */ public static string datetimetodatestringiftimeendzero(date datetime) throws parseexception{ string datetimestring = dateutils.dateformat(datetime, dateutils.date_time_pattern); if(datetimestring.endswith("00:00:00")){ return datetimestring.substring(0, 10); }else{ return datetimestring; } } /** * 将日期时间格式成日期对象,和dateparse互用 * @param datetime date * @return date * @throws parseexception */ public static date datetimetodate(date datetime) throws parseexception{ calendar cal = calendar.getinstance(); cal.settime(datetime); 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(); } /** * 时间加减小时 * @param startdate 要处理的时间,null则为当前时间 * @param hours 加减的小时 * @return date */ public static date dateaddhours(date startdate, int hours) { if (startdate == null) { startdate = new date(); } calendar c = calendar.getinstance(); c.settime(startdate); c.set(calendar.hour, c.get(calendar.hour) + hours); return c.gettime(); } /** * 时间加减分钟 * @param startdate 要处理的时间,null则为当前时间 * @param minutes 加减的分钟 * @return */ public static date dateaddminutes(date startdate, int minutes) { if (startdate == null) { startdate = new date(); } calendar c = calendar.getinstance(); c.settime(startdate); c.set(calendar.minute, c.get(calendar.minute) + minutes); return c.gettime(); } /** * 时间加减秒数 * @param startdate 要处理的时间,null则为当前时间 * @param minutes 加减的秒数 * @return */ public static date dateaddseconds(date startdate, int seconds) { if (startdate == null) { startdate = new date(); } calendar c = calendar.getinstance(); c.settime(startdate); c.set(calendar.second, c.get(calendar.second) + seconds); return c.gettime(); } /** * 时间加减天数 * @param startdate 要处理的时间,null则为当前时间 * @param days 加减的天数 * @return date */ public static date dateadddays(date startdate, int days) { if (startdate == null) { startdate = new date(); } calendar c = calendar.getinstance(); c.settime(startdate); c.set(calendar.date, c.get(calendar.date) + days); return c.gettime(); } /** * 时间加减月数 * @param startdate 要处理的时间,null则为当前时间 * @param months 加减的月数 * @return date */ public static date dateaddmonths(date startdate, int months) { if (startdate == null) { startdate = new date(); } calendar c = calendar.getinstance(); c.settime(startdate); c.set(calendar.month, c.get(calendar.month) + months); return c.gettime(); } /** * 时间加减年数 * @param startdate 要处理的时间,null则为当前时间 * @param years 加减的年数 * @return date */ public static date dateaddyears(date startdate, int years) { if (startdate == null) { startdate = new date(); } calendar c = calendar.getinstance(); c.settime(startdate); c.set(calendar.year, c.get(calendar.year) + years); return c.gettime(); } /** * 时间比较(如果mydate>comparedate返回1,<返回-1,相等返回0) * @param mydate 时间 * @param comparedate 要比较的时间 * @return int */ public static int datecompare(date mydate, date comparedate) { calendar mycal = calendar.getinstance(); calendar comparecal = calendar.getinstance(); mycal.settime(mydate); comparecal.settime(comparedate); return mycal.compareto(comparecal); } /** * 获取两个时间中最小的一个时间 * @param date * @param comparedate * @return */ public static date datemin(date date, date comparedate) { if(date == null){ return comparedate; } if(comparedate == null){ return date; } if(1 == datecompare(date, comparedate)){ return comparedate; }else if(-1 == datecompare(date, comparedate)){ return date; } return date; } /** * 获取两个时间中最大的一个时间 * @param date * @param comparedate * @return */ public static date datemax(date date, date comparedate) { if(date == null){ return comparedate; } if(comparedate == null){ return date; } if(1 == datecompare(date, comparedate)){ return date; }else if(-1 == datecompare(date, comparedate)){ return comparedate; } return date; } /** * 获取两个日期(不含时分秒)相差的天数,不包含今天 * @param startdate * @param enddate * @return * @throws parseexception */ public static int datebetween(date startdate, date enddate) throws parseexception { date datestart = dateparse(dateformat(startdate, date_pattern), date_pattern); date dateend = dateparse(dateformat(enddate, date_pattern), date_pattern); return (int) ((dateend.gettime() - datestart.gettime())/1000/60/60/24); } /** * 获取两个日期(不含时分秒)相差的天数,包含今天 * @param startdate * @param enddate * @return * @throws parseexception */ public static int datebetweenincludetoday(date startdate, date enddate) throws parseexception { return datebetween(startdate, enddate) + 1; } /** * 获取日期时间的年份,如2017-02-13,返回2017 * @param date * @return */ public static int getyear(date date) { calendar cal = calendar.getinstance(); cal.settime(date); return cal.get(calendar.year); } /** * 获取日期时间的月份,如2017年2月13日,返回2 * @param date * @return */ public static int getmonth(date date) { calendar cal = calendar.getinstance(); cal.settime(date); return cal.get(calendar.month) + 1; } /** * 获取日期时间的第几天(即返回日期的dd),如2017-02-13,返回13 * @param date * @return */ public static int getdate(date date) { calendar cal = calendar.getinstance(); cal.settime(date); return cal.get(calendar.date); } /** * 获取日期时间当月的总天数,如2017-02-13,返回28 * @param date * @return */ public static int getdaysofmonth(date date) { calendar cal = calendar.getinstance(); cal.settime(date); return cal.getactualmaximum(calendar.date); } /** * 获取日期时间当年的总天数,如2017-02-13,返回2017年的总天数 * @param date * @return */ public static int getdaysofyear(date date) { calendar cal = calendar.getinstance(); cal.settime(date); return cal.getactualmaximum(calendar.day_of_year); } /** * 根据时间获取当月最大的日期 * <li>2017-02-13,返回2017-02-28</li> * <li>2016-02-13,返回2016-02-29</li> * <li>2016-01-11,返回2016-01-31</li> * @param date date * @return * @throws exception */ public static date maxdateofmonth(date date) throws exception { calendar cal = calendar.getinstance(); cal.settime(date); int value = cal.getactualmaximum(calendar.date); return dateparse(dateformat(date, month_pattern) + "-" + value, null); } /** * 根据时间获取当月最小的日期,也就是返回当月的1号日期对象 * @param date date * @return * @throws exception */ public static date mindateofmonth(date date) throws exception { calendar cal = calendar.getinstance(); cal.settime(date); int value = cal.getactualminimum(calendar.date); return dateparse(dateformat(date, month_pattern) + "-" + value, null); }}
public static void main(string[] args) throws exception { system.out.println(datetimetodate(new date())); system.out.println(dateparse("2017-02-04 14:58:20", null)); system.out.println(datetimetodatestringiftimeendzero(new date())); system.out.println(datetimetodatestringiftimeendzero(datetimetodate(new date()))); system.out.println(datebetween(dateparse("2017-01-30", null), dateparse("2017-02-01", null))); system.out.println(datebetweenincludetoday(dateparse("2017-01-30", null), dateparse("2017-02-01", null))); system.out.println(getdate(dateparse("2017-01-17", null))); system.out.println(getdaysofmonth(dateparse("2017-02-01", null))); system.out.println(getdaysofyear(dateparse("2017-01-30", null))); /* system.out.println(dateformat(dateaddmonths(dateparse("2017-02-07", strutils.month_pattern), -12), strutils.month_pattern));*/ system.out.println(dateformat(maxdateofmonth(dateparse("2016-02", "yyyy-mm")), null)); system.out.println(dateformat(mindateofmonth(dateparse("2016-03-31", null)), null)); }
以上就是java日期工具类的编写方法有哪些的详细内容。
其它类似信息

推荐信息