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;

}

你可能感兴趣的:(SimpleDateFormat的使用)