SimpleDateFormat函數(shù)的繼承關(guān)系:
java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
/**
SimpleDateFormat函數(shù)語法:
G 年代標(biāo)志符
y 年
M 月
d 日
h 時(shí) 在上午或下午 (1~12)
H 時(shí) 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第幾天
F 一月中第幾個(gè)星期幾
w 一年中第幾個(gè)星期
W 一月中第幾個(gè)星期
a 上午 / 下午 標(biāo)記符
k 時(shí) 在一天中 (1~24)
K 時(shí) 在上午或下午 (0~11)
z 時(shí)區(qū)
*/
以下是一些用到的例子:
//拍照的時(shí)候 對生成的照片進(jìn)行命名
public static String getSaveImageFullName() {
return "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";// 照片命名
}
//獲取北京時(shí)間
public static String stringToDateYear(String dateString){
String time = dateString.substring(6,dateString.length()-2);
Date date = new Date(Long.parseLong(time));
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm",new Locale("CHINA"));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
return format.format(date);
}
public static String stringToDate(String dateString){
String time = dateString.substring(6,dateString.length()-2);
Date date = new Date(Long.parseLong(time));
SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm",new Locale("CHINA"));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
return format.format(date);
}
/**
* 調(diào)用此方法輸入所要轉(zhuǎn)換的時(shí)間戳輸入例如(1402733340)輸出("2014-06-14 16:09:00")
*
* @param time
* @return
*/
public static String timedate(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;
}
/**
* 調(diào)用此方法輸入所要轉(zhuǎn)換的時(shí)間戳輸入例如(1402733340)輸出("2014年06月14日16:09")
*
* @param time
* @return
*/
public static String timet(String time) {
? ? SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日? HH:mm");
? ? @SuppressWarnings("unused")
? ? long lcc = Long.valueOf(time);
? ? int i = Integer.parseInt(time);
? ? String times = sdr.format(new Date(i * 1000L));
? ? return times;
}