SimpleDateFormat的使用

SimpleDateFormat函数的继承关系:
java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
/**
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 时区
*/
以下是一些用到的例子:
//拍照的时候 对生成的照片进行命名
public static String getSaveImageFullName() {
return "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";// 照片命名
}

//获取北京时间
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);
}

/**
* 调用此方法输入所要转换的时间戳输入例如(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;

}

/**
 * 调用此方法输入所要转换的时间戳输入例如(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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值