Date now=new Date();
//yyyy年MM月dd日 HH时mm分ss秒 HH为24小时 hh为12小时
//yy/MM/dd HH:mm
//yyyy-MM-dd HH:mm:ss == now.toLocaleString();
格式可以自由组合,不受年月日 : 时分限制等限制 如下:
1 try {
2 SimpleDateFormat format = new SimpleDateFormat("yyyy问你MM年dd HH?mm我ss"); //但不可有字母
3 Date date = format.parse("2014问你12年24 11?20我46");
4 SimpleDateFormat normalformat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
5 String dateStr = normalformat.format(date);
6 System.out.println("dateStr=" +dateStr);
7 }catch (Exception e){
8 System.out.println("e.getMessage()=" +e.getMessage());
9 }
1 try {
2 SimpleDateFormat format = new SimpleDateFormat("yyyy(MM我dd HH?mm我ss");
3 Date date = format.parse("2014(12我24 11?20我46");
4 SimpleDateFormat normalformat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
5 String dateStr = normalformat.format(date);
6 System.out.println("dateStr=" +dateStr);
7 }catch (Exception e){
8 System.out.println("e.getMessage()=" +e.getMessage());
9 }
结果:dateStr=2014年12月24日 11:20:46
1 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒",Locale.ROOT); //Local.root表示显示时间的格式按照系统上当地时间显示,还有Local.Chinese 等
2 //在1989.4.11-1989.9.17期间id为GMT+08:00比id为Asia/Shanghai毫秒值多一个小时,其余时间段相等 修改方案:时区id改为:Asia/Shanghai
3 dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); //可以通过此芳芳设置当前时区为东八区,如不设置,默认是当前手机系统所指时区,GMT+08:00时区在
4 String formatedTimeStr = dateFormat.format(now); //得到当前时间的指定格式
8 Date date2 = dateFormat.parse("2017年11月18日 12时20分"); //解析出date对象获取毫秒值
10 date2.getTime(); //获取毫秒值
1 try{
2
3 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
4 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
5 Log.i("test","str=" + dateFormat.getTimeZone().getID());
6 Date date = dateFormat.parse("1989年4月11日 0时01分00秒");
7 Log.i("test","str=" + date.getTime());
8
9 SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
10 dateFormat2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
11 Log.i("test","str=" + dateFormat2.getTimeZone().getID());
12 // Date date2 = new Date("1989年5月31日0时1分0秒");
13 Date date2 = dateFormat2.parse("1989年4月11日 0时01分00秒"); //1989年9月18日
14 // String str2 = dateFormat2.format(date2);
15 Log.i("test","str=2=" + date2.getTime());
16 }catch (Exception e){
17 e.printStackTrace();
18 }
时间毫秒值与时区
时间毫秒值 指的是0度经线的位置 从1970.1.1日0点0分0秒开始毫秒值
对于同一个毫秒值,世界各地(24个)时区东1-东12,西1-西12 各个时区对应时间不一样
向东每个时区逐+1小时,向西每个时区逐减1小时
例如:在0度经线对应2017.11.18 12时30分30秒,在东1区对应 2017.11.18 13时30分30秒,在西1区对应时间2017.11.18 11时30分30秒 ,以此类推
同一个时间,在各个时区,对应的毫秒值也不同 , 比如:2017.11.18 12时30分30秒, 东12区对应的毫秒值最小,西12区对应的毫秒值最大。

