SimpleDateFormate日期字符串转换(墨雪亲测)
2016-12-05 10:06
288 查看
1.字符串日期转换为date型:
2.date/calendar类型转换为指定格式日期:
效果:
2004年12月16日 17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17时24分27秒 星期四
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
3.long型的时间与字符串时间的转换:
//SimpleDateFormat中的parse方法可以 //把String型的字符串转换成特定格式的date类型 public static void main(String[] args) { String dStr = "2001.12.12-08:23:21"; Date d = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss"); try { d = sdf.parse(dStr); } catch (ParseException pe) { System.out.println(pe.getMessage()); } System.out.println(d); System.out.println(d.getTime()); }
2.date/calendar类型转换为指定格式日期:
public class FormatDateTime { public static void main(String[] args) { SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString() SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E "); SimpleDateFormat myFmt4=new SimpleDateFormat( "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区"); Date now=new Date(); System.out.println(myFmt.format(now)); System.out.println(myFmt1.format(now)); System.out.println(myFmt2.format(now)); System.out.println(myFmt3.format(now)); System.out.println(myFmt4.format(now)); System.out.println(now.toGMTString()); System.out.println(now.toLocaleString()); System.out.println(now.toString()); } }
效果:
2004年12月16日 17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17时24分27秒 星期四
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
3.long型的时间与字符串时间的转换:
public class TimeFormat { //输入参数:2016年12月5日10时55分23秒 周一,得到字符串:1480906523000 public String timeStr_TO_longStr(String time_str) { String dStr = time_str; String long_str; Date d = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒 E"); try { d = sdf.parse(dStr); } catch (ParseException pe) { System.out.println(pe.getMessage()); } Calendar c=Calendar.getInstance(); c.setTime(d); long_str=c.getTimeInMillis()+""; return long_str; } //输入参数:1480906523000,得到字符串:2016年12月5日10时55分23秒 周一 public String longStr_TO_timeStr(String long_str){ String longStr = long_str; long l_value=Long.parseLong(longStr); Calendar c=Calendar.getInstance(); c.setTimeInMillis(l_value); String timeStr; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒 E"); timeStr = sdf.format(c.getTime()); return timeStr; } }
相关文章推荐
- Java日期转换SimpleDateFormat格式大全 和 判断是否为合法的日期时间字符串?
- 4.4.2 将字符串转换为日期SimpleDateFormat类
- 使用SimpleDateFormate转换日期出错
- java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
- java---SimpleDateFormat---实现字符串和日期类型互相转换
- 日期字符串解析--SimpleDateFormat严格限制日期转换setLenient(false)
- java中使用SimpleDateFormat实现字符串和日期的相互转换
- java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
- 使用DateLocaleConverter和SimpleDateFormat实现字符串转换成日期的区别
- 日期字符串解析--SimpleDateFormat严格限制日期转换setLenient(false)
- java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
- javascript中如何正确将日期(Date)字符串转换为日期(Date)对象?
- SimpleDateFormat严格限制日期转换setLenient(false)
- mysql str_to_date字符串转换为日期
- 在java中实现日期类型和字符串类型的转换_所有(Date String Timestamp Datetime)
- javascript日期字符串转换为Date对象
- javascript中如何正确将日期(Date)字符串,转换为日期(Date)对象?
- 将Fri May 04 17:25:34 CST 2012形式的日期字符串转换成java.util.Date对象的方法
- JavaScript将字符串转换成日期的方法(JavaScript String to Date)
- oracle 中to_date() 与 to_char() 日期和字符串转换