1. 程式人生 > >Java String和Date的轉換 轉http://www.cnblogs.com/bmbm/archive/2011/12/06/2342264.html

Java String和Date的轉換 轉http://www.cnblogs.com/bmbm/archive/2011/12/06/2342264.html

ref integer public cat 標記 星期 import 轉換 star

Java String和Date的轉換

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. import org.apache.commons.lang.StringUtils;
  6. /**
  7. * 日期Util類
  8. *
  9. * @author calvin
  10. */
  11. public class DateUtil
  12. {
  13. private static String defaultDatePattern = "yyyy-MM-dd ";
  14. /**
  15. * 獲得默認的 date pattern
  16. */
  17. public static String getDatePattern()
  18. {
  19. return defaultDatePattern;
  20. }
  21. /**
  22. * 返回預設Format的當前日期字符串
  23. */
  24. public static String getToday()
  25. {
  26. Date today = new Date();
  27. return format(today);
  28. }
  29. /**
  30. * 使用預設Format格式化Date成字符串
  31. */
  32. public static String format(Date date)
  33. {
  34. return date == null ? " " : format(date, getDatePattern());
  35. }
  36. /**
  37. * 使用參數Format格式化Date成字符串
  38. */
  39. public static String format(Date date, String pattern)
  40. {
  41. return date == null ? " " : new SimpleDateFormat(pattern).format(date);
  42. }
  43. /**
  44. * 使用預設格式將字符串轉為Date
  45. */
  46. public static Date parse(String strDate) throws ParseException
  47. {
  48. return StringUtils.isBlank(strDate) ? null : parse(strDate,
  49. getDatePattern());
  50. }
  51. /**
  52. * 使用參數Format將字符串轉為Date
  53. */
  54. public static Date parse(String strDate, String pattern)
  55. throws ParseException
  56. {
  57. return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(
  58. pattern).parse(strDate);
  59. }
  60. /**
  61. * 在日期上增加數個整月
  62. */
  63. public static Date addMonth(Date date, int n)
  64. {
  65. Calendar cal = Calendar.getInstance();
  66. cal.setTime(date);
  67. cal.add(Calendar.MONTH, n);
  68. return cal.getTime();
  69. }
  70. public static String getLastDayOfMonth(String year, String month)
  71. {
  72. Calendar cal = Calendar.getInstance();
  73. // 年
  74. cal.set(Calendar.YEAR, Integer.parseInt(year));
  75. // 月,因為Calendar裏的月是從0開始,所以要-1
  76. // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
  77. // 日,設為一號
  78. cal.set(Calendar.DATE, 1);
  79. // 月份加一,得到下個月的一號
  80. cal.add(Calendar.MONTH, 1);
  81. // 下一個月減一為本月最後一天
  82. cal.add(Calendar.DATE, -1);
  83. return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 獲得月末是幾號
  84. }
  85. public static Date getDate(String year, String month, String day)
  86. throws ParseException
  87. {
  88. String result = year + "- "
  89. + (month.length() == 1 ? ("0 " + month) : month) + "- "
  90. + (day.length() == 1 ? ("0 " + day) : day);
  91. return parse(result);
  92. }
  93. }

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,不需要另外的函數。

Java String和Date的轉換 轉http://www.cnblogs.com/bmbm/archive/2011/12/06/2342264.html