`
youyu4
  • 浏览: 425422 次
社区版块
存档分类
最新评论

Java中Date和String转换

    博客分类:
  • java
阅读更多

String—>Date方法一:

 

  1. String dateString = "2012-12-06 ";  
  2. try  
  3. {  
  4.     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");  
  5.     Date date = sdf.parse(dateString);  
  6. }  
  7. catch (ParseException e)  
  8. {  
  9.     System.out.println(e.getMessage());  
  10. }  

 

 

String—>Date方法二:

 

  1. import java.text.ParseException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5.   
  6. import org.apache.commons.lang.StringUtils;  
  7.   
  8. /** 
  9.  * 日期Util类 
  10.  *  
  11.  * @author calvin 
  12.  */  
  13. public class DateUtil  
  14. {  
  15.     private static String defaultDatePattern = "yyyy-MM-dd ";  
  16.   
  17.     /** 
  18.      * 获得默认的 date pattern 
  19.      */  
  20.     public static String getDatePattern()  
  21.     {  
  22.         return defaultDatePattern;  
  23.     }  
  24.   
  25.     /** 
  26.      * 返回预设Format的当前日期字符串 
  27.      */  
  28.     public static String getToday()  
  29.     {  
  30.         Date today = new Date();  
  31.         return format(today);  
  32.     }  
  33.   
  34.     /** 
  35.      * 使用预设Format格式化Date成字符串 
  36.      */  
  37.     public static String format(Date date)  
  38.     {  
  39.         return date == null ? " " : format(date, getDatePattern());  
  40.     }  
  41.   
  42.     /** 
  43.      * 使用参数Format格式化Date成字符串 
  44.      */  
  45.     public static String format(Date date, String pattern)  
  46.     {  
  47.         return date == null ? " " : new SimpleDateFormat(pattern).format(date);  
  48.     }  
  49.   
  50.     /** 
  51.      * 使用预设格式将字符串转为Date 
  52.      */  
  53.     public static Date parse(String strDate) throws ParseException  
  54.     {  
  55.         return StringUtils.isBlank(strDate) ? null : parse(strDate,  
  56.                 getDatePattern());  
  57.     }  
  58.   
  59.     /** 
  60.      * 使用参数Format将字符串转为Date 
  61.      */  
  62.     public static Date parse(String strDate, String pattern)  
  63.             throws ParseException  
  64.     {  
  65.         return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(  
  66.                 pattern).parse(strDate);  
  67.     }  
  68.   
  69.     /** 
  70.      * 在日期上增加数个整月 
  71.      */  
  72.     public static Date addMonth(Date date, int n)  
  73.     {  
  74.         Calendar cal = Calendar.getInstance();  
  75.         cal.setTime(date);  
  76.         cal.add(Calendar.MONTH, n);  
  77.         return cal.getTime();  
  78.     }  
  79.   
  80.     public static String getLastDayOfMonth(String year, String month)  
  81.     {  
  82.         Calendar cal = Calendar.getInstance();  
  83.         // 年  
  84.         cal.set(Calendar.YEAR, Integer.parseInt(year));  
  85.         // 月,因为Calendar里的月是从0开始,所以要-1  
  86.         // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);  
  87.         // 日,设为一号  
  88.         cal.set(Calendar.DATE, 1);  
  89.         // 月份加一,得到下个月的一号  
  90.         cal.add(Calendar.MONTH, 1);  
  91.         // 下一个月减一为本月最后一天  
  92.         cal.add(Calendar.DATE, -1);  
  93.         return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号  
  94.     }  
  95.   
  96.     public static Date getDate(String year, String month, String day)  
  97.             throws ParseException  
  98.     {  
  99.         String result = year + "- "  
  100.                 + (month.length() == 1 ? ("0 " + month) : month) + "- "  
  101.                 + (day.length() == 1 ? ("0 " + day) : day);  
  102.         return parse(result);  
  103.     }  
  104. }  

Date—>String

  1. String sdate;  
  2. Date ddate;  
  3. ……  
  4. sdate=(new SimpleDateFormat("yyyy-MM-dd")).format(ddate);  

SimpleDateFormat函数语法:
         G 年代标志符
         y 年
         M 月
         d 日
         h 时 在上午或下午 (1~12)
         H 时 在一天中 (0~23)
         m 分
         s 秒
         S 毫秒
         E 星期
         D 一年中的第几天
         F 一月中第几个星期几
         w 一年中第几个星期
         W 一月中第几个星期
         a 上午 / 下午 标记符
         k 时 在一天中 (1~24)
         K 时 在上午或下午 (0~11)
         z 时区
常见标准的写法"yyyy-MM-dd HH:mm:ss",注意大小写,时间是24小时制,24小时制转换成12小时制只需将HH改成hh,不需要另外的函数。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics