Java中使用SimpleDateFormat类的构造函数SimpleDateFormat(String str)构造格式化日期的格式,通过format(Date date)方法将指定的日期(Date)对象格式化为指定格式的字符串。
SimpleDateFormat构造函数中字符串的格式,以及各部分代表的含义:
import java.text.SimpleDateFormat;
import java.util.Date;
public class test{
public static void main(String args[]) {
Date newTime = new Date();
//设置时间格式
SimpleDateFormat sdf1 = new SimpleDateFormat("y-M-d h:m:s a E");
SimpleDateFormat sdf2 = new SimpleDateFormat("yy-MM-dd hh:mm:ss a E");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MMM-ddd hhh:mmm:sss a E");
SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyy-MMMM-dddd hhhh:mmmm:ssss a E");
//获取的时间,是本机的时间
String formatDate1 = sdf1.format(newTime);
String formatDate2 = sdf2.format(newTime);
String formatDate3 = sdf3.format(newTime);
String formatDate4 = sdf4.format(newTime);
System.out.println(formatDate1);
System.out.println(formatDate2);
System.out.println(formatDate3);
System.out.println(formatDate4);
}
}
运行结果:

一、字符串"yyyy-MM-dd hh:mm:ss",其中:
yyyy:代表年(不去区分大小写)假设年份为 2017
- "y" , "yyy" , "yyyy" 匹配的都是4位完整的年 如:"2017"
- "yy" 匹配的是年分的后两位 如:"15"
- 超过4位,会在年份前面加"0"补位,如 "YYYYY"对应"02017"
MM:代表月(只能使用大写)假设月份为 9
- "M" 对应 "9"(1-2位显示)
- "MM" 对应 "09"(2位数表示)
- "MMM" 对应 "Sep"(显示为英文月份简写)
- "MMMM" 对应 "September"(显示为英文月份全称)
- 超出3位,仍然对应 "September"
dd:代表日(只能使用小写)假设为03号
- "d" 对应 "3"(1-2位显示)
- "dd" 对应 "03"(2位数表示)
- 超出2位,会在数字前面加"0"补位,例如 "dddd" 对应 "0013"
hh:代表时(区分大小写,大写为24进制计时,小写为12进制计时)假设为05时
- "H" 对应 "5"
- "HH" 对应 "05" ,超出2位,会在数字前面加"0"补位,例如 "HHHH" 对应 "0005"
- "h" 对应 "3"
- "hh" 对应 "03",超出2位,会在数字前面加"0"补位,例如 "hhhh" 对应 "0003"
mm:代表分(只能使用小写)假设为02分
- "m" 对应 "2"(1-2位显示)
- "mm" 对应 "02"(2位数表示)
- 超出2位,会在数字前面加"0"补位。例如 "mmmm" 对应 "0032"
ss : 代表秒(只能使用小写)假设为05秒
- "s" 对应 "5"(1-2位显示)
- "ss" 对应 "15"(2位数表示)
- 超出2位,会在数字前面加"0"补位,例如 "ssss" 对应 "0015"
E : 代表星期(只能使用大写)假设为 Sunday
- "E" , "EE" , "EEE" 都对应 "Sun"(简写星期几)
- "EEEE" 对应 "Sunday",超出4位,仍然对应 "Sunday"(全写星期几)
a : 代表上午还是下午,如果是上午就对应 "AM" ,如果是下午就对应 "PM"
其中的分隔符"-"可以替换成其他非字母的任意字符(也可以是汉字),例如:
部分修改:

运行结果:

二、日期和时间的格式化编码
时间模式字符串用来指定时间格式。在此模式中,所有的 ASCII 字母被保留为模式字母,定义如下:
| 字母 | 描述 | 示例 |
|---|---|---|
| G | 纪元标记 | AD |
| y | 四位年份 | 2001 |
| M | 月份 | July or 07 |
| d | 一个月的日期 | 10 |
| h | A.M./P.M. (1~12)格式小时 | 12 |
| H | 一天中的小时 (0~23) | 22 |
| m | 分钟数 | 30 |
| s | 秒数 | 55 |
| S | 毫秒数 | 234 |
| E | 星期几 | Tuesday |
| D | 一年中的日子 | 360 |
| F | 一个月中第几周的周几 | 2 (second Wed. in July) |
| w | 一年中第几周 | 40 |
| W | 一个月中第几周 | 1 |
| a | A.M./P.M. 标记 | PM |
| k | 一天中的小时(1~24) | 24 |
| K | A.M./P.M. (0~11)格式小时 | 10 |
| z | 时区 | Eastern Standard Time |
| ' | 文字定界符 | Delimiter |
| " | 单引号 | ` |
三、解析英文格式的月份日期
英文格式的时间转换时需要带上Locale.ENGLISH,否则会转换失败,因为它默认的是本地化的设置,除非你的操作系统是英文的,总之时间转换时需要时间格式与模式保持一致。
// 由于要解析的日期中有Mar这样的英文,所以用下面这个格式
//(需要指定第二个参数值为:Locale.ENGLISH)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);
Date date = simpleDateFormat.parse("06/Mar/2017:00:43:07");
// 由于Date中的很多方法已经不建议使用,所以变成GregorianCalendar
GregorianCalendar calendar=new GregorianCalendar();
calendar.setTime(date);
System.out.println(calendar.get(Calendar.MONTH));
参考: