爪哇国新游记之三十一----日期时间与字符串间的转化
生活随笔
收集整理的這篇文章主要介紹了
爪哇国新游记之三十一----日期时间与字符串间的转化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.由日期時間轉化成字符串
Date date = new Date(); Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString=formatter.format(date);上述代碼使用的是SimpleDateFormat的format函數
?
2.由字符串轉化成日期時間
String dateStr1="20141216"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date1 = sdf.parse(dateStr1);上述代碼使用的是SimpleDateFormat的parse函數。
?
3.得到幾天前的日期
public static String getDateBefore(Date d, int day) {Calendar now = Calendar.getInstance();now.setTime(d);now.set(Calendar.DATE, now.get(Calendar.DATE) - day);Format formatter = new SimpleDateFormat("yyyy-MM-dd");return formatter.format(now.getTime());}?
4.得到一個月的第一天,這個比較簡單
String endDate="2014-12-22"; String[] arr=endDate.split("-"); String startDate=arr[0]+"-"+arr[1]+"-"+"01";?
5.得到一周的第一天
String newEndDate="2014-12-22";SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date=fmt.parse(newEndDate); Calendar c=Calendar.getInstance(); c.setTime(date); int weekday=c.get(Calendar.DAY_OF_WEEK);String startDate=DateTimeUtil.getDateBefore(date, weekday-1);?
本文轉自張昺華-sky博客園博客,原文鏈接:http://www.cnblogs.com/xiandedanteng/p/4166264.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的爪哇国新游记之三十一----日期时间与字符串间的转化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云云主机添加swap分区与swap性
- 下一篇: 权限控制框架Shiro简单介绍及配置实例