《JAVA字符串转日期或日期转字符串 .docx》由会员分享,可在线阅读,更多相关《JAVA字符串转日期或日期转字符串 .docx(26页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、精品名师归纳总结JAVA字符串转日期或日期转字符串 转2021-08-16 16:34:03|分类: java | 字号 订阅JAVA字符串转日期或日期转字符串 转文章中,用的 API 是 SimpleDateFormat ,它是属于java.text.SimpleDateFormat,所以请记得 import进来!用法: SimpleDateFormat sdf =newSimpleDateFormat yyyy-MM-dd HH:mm:ss ;这一行最重要,它确立了转换的格式,yyyy 是完整的公元年,MM是月份, dd 是日期,至于 HH:mm:ss就不需要我再说明白吧!PS:为什么有的
2、格式大写,有的格式小写,那是怕防止混淆, 例如 MM是月份, mm是分。 HH是 24 小时制,而 hh 是 12 小时制。1. 字符串转日期2021-07-10 19:20:00要把它转成日期,可以用 Date date = sdf.parse 2021-07-10 19:20:00 ;2. 日期转字符串假如把今日的日期转成字符串可用String str = sdf.formatnew Date;这个字符串内容的格式类似 2021-07-10 19:20:00。透过这个 API 我们便可以随心所欲的将日期转成我们想要的字符串格式,例如期望将日期输出成2021年 7 月 10 日,我们可以这么
3、写:SimpleDateFormatsdf =new SimpleDateFormat yyyy年 MM月 dd 日 ;String str = sdf.formatnew Date;可编辑资料 - - - 欢迎下载精品名师归纳总结str便会依照我们设定的格式输出了。附编写好的一个简洁实例:import java.util.Date;import java.text.ParseException; import java.text.SimpleDateFormat;public class ConvertDemo /* 日期转换成字符串* param date* return str*/pub
4、lic static String DateToStrDate date SimpleDateFormat format = new SimpleDateFormatyyyy-MM-dd HH:mm:ss;String str = format.formatdate; return str;/* 字符串转换成日期* param str* return date*/public static Date StrToDateString str SimpleDateFormat format = new SimpleDateFormatyyyy-MM-dd HH:mm:ss;Date date =
5、null; try date = format.parsestr; catch ParseException e e.printStackTrace;return date;public static void mainString args Date date = new Date;可编辑资料 - - - 欢迎下载精品名师归纳总结System.out.println日期转字符串: + ConvertDemo.DateToStrdate;System.out.println字符串转日期: + ConvertDemo.StrToDateConvertDemo.DateToStrdate;*下面的
6、都是例子*Java 中日期格式转换可编辑资料 - - - 欢迎下载精品名师归纳总结/* 字符串转换为 java.util.Date* 支持格式为 yyyy.MM.dd G at hh:mm:ss z如 2002-1-1 AD at可编辑资料 - - - 欢迎下载精品名师归纳总结22:10:59 PSD* yy/MM/dd HH:mm:ss如 2002/1/1 17:55:00* yy/MM/dd HH:mm:ss pm如 2002/1/1 17:55:00 pm* yy-MM-dd HH:mm:ss如 2002-1-1 17:55:00 * yy-MM-dd HH:mm:ss am如 2002
7、-1-1 17:55:00 am * param time String字符串 * return Date日期*/public static Date stringToDateString time SimpleDateFormat formatter;int tempPos=time.indexOfAD ;time=time.trim ;formatter = new SimpleDateFormat yyyy.MM.dd G at hh:mm:ss可编辑资料 - - - 欢迎下载精品名师归纳总结z;iftempPos-1time=time.substring0,tempPos+ 公元可编辑
8、资料 - - - 欢迎下载精品名师归纳总结+time.substringtempPos+AD.length;/chinaformatter= newSimpleDateFormat yyyy.MM.ddGathh:mm:ss可编辑资料 - - - 欢迎下载精品名师归纳总结z;tempPos=time.indexOf-; iftempPos-1&time.indexOf -1 &time.indexOf -1formatter = new SimpleDateFormat yyyy/MM/dd HH:mm:ss;else iftime.indexOf-1 &time.indexOf -1form
9、atter = new SimpleDateFormat yyyy-MM-dd HH:mm:ss;else iftime.indexOf/-1 &time.indexOfam-1|time.indexOfpm-1formatter = new SimpleDateFormat yyyy-MM-dd KK:mm:ss a;else iftime.indexOf-1 &time.indexOfam-1|time.indexOfpm-1formatter = new SimpleDateFormat yyyy-MM-dd KK:mm:ss a;ParsePosition pos = new Pars
10、ePosition0; java.util.Date ctime = formatter.parsetime, pos;可编辑资料 - - - 欢迎下载精品名师归纳总结/*return ctime;* 将 java.util.Date格式转换为字符串格式 yyyy-MM-dd HH:mm:ss24可编辑资料 - - - 欢迎下载精品名师归纳总结小时制 *如 Sat May 11 17:24:21 CST 2002 to 2002-05-11 17:24:21* param time Date日期* return String字符串*/public static String dateToStr
11、ingDate time SimpleDateFormat formatter;formatter = new SimpleDateFormat yyyy-MM-dd HH:mm:ss; String ctime = formatter.formattime;可编辑资料 - - - 欢迎下载精品名师归纳总结/*return ctime;* 将 java.util.Date格式转换为字符串格式 yyyy-MM-dd HH:mm:ss可编辑资料 - - - 欢迎下载精品名师归纳总结a12小时制 *如 Sat May11 17:23:22CST2002 to 2002-05-1105:23:22下午
12、* param time Date日期可编辑资料 - - - 欢迎下载精品名师归纳总结* param x int任意整数如: 1* return String字符串*/public static String dateToStringDate time,int x SimpleDateFormat formatter;formatter = new SimpleDateFormat yyyy-MM-dd KK:mm:ss a; String ctime = formatter.formattime;可编辑资料 - - - 欢迎下载精品名师归纳总结/*return ctime;* 取系统当前时间
13、 : 返回只值为如下形式*2002-10-30 20:24:39* return String*/可编辑资料 - - - 欢迎下载精品名师归纳总结public static String Nowreturn dateToStringnew Date;可编辑资料 - - - 欢迎下载精品名师归纳总结/* 取系统当前时间 : 返回只值为如下形式*2002-10-30 08:28:56下午*param hour为任意整数*return String*/可编辑资料 - - - 欢迎下载精品名师归纳总结public static String Nowint hourreturn dateToString
14、new Date,hour;可编辑资料 - - - 欢迎下载精品名师归纳总结/* 取系统当前时间 : 返回值为如下形式*2002-10-30*return String*/可编辑资料 - - - 欢迎下载精品名师归纳总结public static String getYYYY_MM_DDreturn dateToStringnew Date.substring0,10;/* 取系统给定时间 : 返回值为如下形式可编辑资料 - - - 欢迎下载精品名师归纳总结*2002-10-30*return String*/public static String getYYYY_MM_DDString d
15、ate return date.substring0,10;public static String getHour SimpleDateFormat formatter;formatter = new SimpleDateFormat H; String ctime = formatter.formatnew Date; return ctime;public static String getDaySimpleDateFormat formatter; formatter = new SimpleDateFormat d;String ctime = formatter.formatnew
16、 Date; return ctime;public static String getMonth SimpleDateFormat formatter;formatter = new SimpleDateFormat M; String ctime = formatter.formatnew Date; return ctime;public static String getYear SimpleDateFormat formatter;formatter = new SimpleDateFormat yyyy;String ctime = formatter.formatnew Date
17、; return ctime;public static String getWeek SimpleDateFormat formatter;formatter = new SimpleDateFormat E;String ctime = formatter.formatnew Date; return ctime;在 jsp 页面中的日期格式和 sqlserver中的日期格式不一样,怎样统一?可编辑资料 - - - 欢迎下载精品名师归纳总结在页面上显示输出时,用下面的函数处理一下public class DateUtilpublic static String fmtShortEnuDat
18、e myDate SimpleDateFormat formatter = newSimpleDateFormatyyyy/MM/dd;String strDate = formatter.formatmyDate; return strDate;new java.text.SimpleDateFormatyyyy-MM-dd HH:mm:ss; new java.text.SimpleDateFormatyyyy-MM-dd建议仍是把 sqlserver的字段类型改成 varchar的吧,用字符串处理可以完全依据自己的意愿处理,没有特殊的需求,不要使用date 型字串日期格式转换用的 API
19、 是 SimpleDateFormat ,它是属於 java.text.SimpleDateFormat,所以请记得 import进来!用法:SimpleDateFormat sdf=new SimpleDateFormatyyyy-MM-dd HH:mm:ss;这一行最重要,它确立了转换的格式, yyyy 是完整的西元年, MM是月份, dd 是日期, 至於 HH:mm:ss就不需要我再说明白吧!ps: 为什麽有的格式大写,有的格式小写,那是怕防止混淆,例如MM是月份,mm是分。 HH是 24 小时制,而 hh 是 12 小时制1. 字串转日期:2002-10-8 15:30:22要把它转成
20、日期,可以用Date date=sdf.parse2002-10-8 15:30:22;2. 日期转字串假如把今日的日期转成字串可用String datestr=sdf.formatnew Date;这个字串的内容便类似2002-10-08 14:55:38透过这个 API 我们便可以随心所欲的将日期转成我们想要的字串格式,例如期望将日期输出成 2002 年 10 月 08 日,我们可以这麽写:SimpleDateFormat sdf=new SimpleDateFormatyyyy年 MM月 dd 日; String datestr=sdf.formatnew Date;datestr便会依
21、照我们设定的格式输出/ 对日期格式的转换成( yyyy-MM-dd )格式的方法可编辑资料 - - - 欢迎下载精品名师归纳总结public java.sql.Date ConvertString strjava.text.SimpleDateFormat sdf = new java.text.SimpleDateFormatyyyy-MM-dd;tryjava.util.Date d = sdf.parsestr;java.sql.Date d1 = new java.sql.Dated.getTime; return d1;catchException exex.printStackTr
22、ace; return null;应用如下:ctmt.setDate7,this.Convertinfo.getManBirth; / DATETIME常用日期问题集锦1、猎取服务器端当前日期:2、猎取当前年、月、日:3、按本的时区输出当前日期输出结果为:2003-5-304、猎取数据库中字段名为” publish_time“、类型为 Datetime 的值code5、依据指定格式打印日期code输出的结果为:It is星期五 2003.05.30 at 11:30:46上午 CST 更为详尽的格式符号请参看 SimpleDateFormat 类6、将字符串转换为日期输出结果为:Fri Nov
23、 11 00:00:00 CST 12227、运算日期之间的间隔输出结果为:Difference is 29 days.8、日期的加减运算方法:用 Calendar 类的 add 方法%Calendar now = Calendar.getInstance;SimpleDateFormat formatter = new SimpleDateFormatE yyyy.MM.dd at hh:mm:ss a zzz;out.printlnIt is now + formatter.formatnow.getTime;now.addCalendar.DAY_OF_YEAR,-365*2; out.
24、println;out.printlnTwo years ago was + formatter.formatnow.getTime;%输出结果为:It is now星期五 2003.05.30 at 01:45:32下午 CSTTwo years ago was星期三 2001.05.30 at 01:45:32下午 CST9、比较日期方法:用 equals、before、after方法可编辑资料 - - - 欢迎下载精品名师归纳总结输出结果为:Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 199910、记录一件事
25、所花费的时间方法:调用两次 System.getTimeMillis方法,求差值%long t0,t1;t0 = System.currentTimeMillis; out.printlnCyc starts at + t0; int k = 0;forint i =0;i100000;i+ k += i;t1 = System.currentTimeMillis; out.println; out.printlnCyc ends at + t1; out.println;out.printlnThis run took + t1-t0 + ms.;%输出结果为:Cyc starts at 1
26、054275312432 Cyc ends at 1054275312442 This run took 10ms.其它:如何格式化小数可编辑资料 - - - 欢迎下载精品名师归纳总结输出结果为:33,665,448,856.66=日期比较:在 JAVA中日期的运算与比较可以使用Date 和 DateFormat 来解决,下面是一段示例代码:import java.text.*; import java.util.*;public class Testpublic static void mainString args tryDate date=new Date;DateFormat df=D
27、ateFormat.getDateTimeInstance; String now=df.formatdate;System.out.println现在时间 :+now;System.out.println现在时间是否在 16:00 之前:+date.beforedf.parse2004-12-24 16:00:00;catchParseException eSystem.out.printe.getMessage;*有是一段例子*可编辑资料 - - - 欢迎下载精品名师归纳总结小例 1:import java.text.ParseException; import java.text.Sim
28、pleDateFormat; import java.util.Date;public class Text public static void mainString argsSimpleDateFormat dd=new可编辑资料 - - - 欢迎下载精品名师归纳总结SimpleDateFormatyyyy-MM-dd;String d=dd.formatnew Date; System.out.printd;try Date date=dd.parse2007-12-12; System.out.printlndate; catch ParseException e e.printSta
29、ckTrace;下面是把 Int整数转换成 Dateimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;public class Text public static void mainString argsSimpleDateFormat dd=new SimpleDateFormatyyyyMMdd; String d=dd.formatnew Date;System.out.printd; int ddd=20071212; try Date date=dd.p
30、arseddd; System.out.printlndate; catch ParseException e e.printStackTrace;小例 2:如何猎取当前时间?String now=new SimpleDateFormatyyyy-MM-dd HH:mm:ss.formatCalendar.getInstance.getTime;Date createDate=convertDatenow; /转换成 Date 型/将字符串转换成日期型的方法。public Date convertDateString planedDate可编辑资料 - - - 欢迎下载精品名师归纳总结Date
31、 date=new Date;SimpleDateFormat dd=new SimpleDateFormatyyyy-MM-dd; String d=dd.formatnew Date;System.out.printd; try date=dd.parseplanedDate; System.out.printlndate; catch ParseException e e.printStackTrace; return null;return date;可编辑资料 - - - 欢迎下载精品名师归纳总结小例 3:如何获得系统的 year,month,day?Calendarc=Calend
32、ar.getInstance; c.setTimenew java.util.Date;intyear =c.getCalendar.YEAR;intmonth =c.getCalendar.MONTH+1;intday=c.getCalendar.DAY_OF_MONTH; inthour =c.getCalendar.HOUR_OF_DAY;intminute =c.getCalendar.MINUTE; intsecond =c.getCalendar.SECOND;可编辑资料 - - - 欢迎下载精品名师归纳总结可编辑资料 - - - 欢迎下载精品名师归纳总结小例 4:JAVA 中获得
33、本的系统时间的方法import java.util.*;public class Dpublic static void mainString abcint y,m,d,h,mi,s;Calendar cal=Calendar.getInstance; y=cal.getCalendar.YEAR; m=cal.getCalendar.MONTH; d=cal.getCalendar.DATE; h=cal.getCalendar.HOUR_OF_DAY; mi=cal.getCalendar.MINUTE; s=cal.getCalendar.SECOND;System.out.println现在时刻是 +y+ 年+m+月+d+ 日+h+可编辑资料 - - - 欢迎下载精品名师归纳总结时+mi+ 分+s+ 秒;可编辑资料 - -