Java String和Date的轉換 轉http://www.cnblogs.com/bmbm/archive/2011/12/06/2342264.html
阿新 • • 發佈:2017-06-25
ref integer public cat 標記 星期 import 轉換 star
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的轉換
String—>Date方法一:
- String dateString = "2012-12-06 ";
- try
- {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
- Date date = sdf.parse(dateString);
- }
- catch (ParseException e)
- {
- System.out.println(e.getMessage());
- }
String—>Date方法二:
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import org.apache.commons.lang.StringUtils;
- /**
- * 日期Util類
- *
- * @author calvin
- */
- public class DateUtil
- {
- private static String defaultDatePattern = "yyyy-MM-dd ";
- /**
- * 獲得默認的 date pattern
- */
- public static String getDatePattern()
- {
- return defaultDatePattern;
- }
- /**
- * 返回預設Format的當前日期字符串
- */
- public static String getToday()
- {
- Date today = new Date();
- return format(today);
- }
- /**
- * 使用預設Format格式化Date成字符串
- */
- public static String format(Date date)
- {
- return date == null ? " " : format(date, getDatePattern());
- }
- /**
- * 使用參數Format格式化Date成字符串
- */
- public static String format(Date date, String pattern)
- {
- return date == null ? " " : new SimpleDateFormat(pattern).format(date);
- }
- /**
- * 使用預設格式將字符串轉為Date
- */
- public static Date parse(String strDate) throws ParseException
- {
- return StringUtils.isBlank(strDate) ? null : parse(strDate,
- getDatePattern());
- }
- /**
- * 使用參數Format將字符串轉為Date
- */
- public static Date parse(String strDate, String pattern)
- throws ParseException
- {
- return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(
- pattern).parse(strDate);
- }
- /**
- * 在日期上增加數個整月
- */
- public static Date addMonth(Date date, int n)
- {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.MONTH, n);
- return cal.getTime();
- }
- public static String getLastDayOfMonth(String year, String month)
- {
- Calendar cal = Calendar.getInstance();
- // 年
- cal.set(Calendar.YEAR, Integer.parseInt(year));
- // 月,因為Calendar裏的月是從0開始,所以要-1
- // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
- // 日,設為一號
- cal.set(Calendar.DATE, 1);
- // 月份加一,得到下個月的一號
- cal.add(Calendar.MONTH, 1);
- // 下一個月減一為本月最後一天
- cal.add(Calendar.DATE, -1);
- return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 獲得月末是幾號
- }
- public static Date getDate(String year, String month, String day)
- throws ParseException
- {
- String result = year + "- "
- + (month.length() == 1 ? ("0 " + month) : month) + "- "
- + (day.length() == 1 ? ("0 " + day) : day);
- return parse(result);
- }
- }
Date—>String
- String sdate;
- Date ddate;
- ……
- sdate=(new SimpleDateFormat("yyyy-MM-dd")).format(ddate);
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