将日期指定格式化 SimpleDateFormat Calendar

   import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 
 4 public class Demo{
 5     public static void main(String[] args) {
 6         Date d = new Date();
 7         System.out.println(d); //无格式
 8         
 9         //将模式封装到SimpleDateFormat对象中
10         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E  HH:mm:ss");
11         
12         //调用format方法,让模式格式化指定Date对象
13         String time = sdf.format(d);
14         System.out.println(time);
15     }
16     
17 }

结果为:

Fri Jan 20 21:39:27 GMT+08:00 2017
2017年01月20日 星期五  21:39:27
1 import java.util.Calendar;
 2 
 3 public class Demo{
 4     public static void main(String[] args) {
 5         Calendar c = Calendar.getInstance();
 6         System.out.println(c.get(Calendar.YEAR) + "");
 7         
 8         //获取任意年的二月有多少天
 9         c.set(2017, 2, 1); //设置某一年3月1日
10         c.add(Calendar.DAY_OF_MONTH, -1);  //3月1日往前推一天,就是2月最后一天
11         System.out.println(c.get(Calendar.DAY_OF_MONTH));
12     }
13 }

结果为:

2017年
28
 
posted @ 2017-06-02 10:30  狮子无敌514  阅读(314)  评论(0编辑  收藏  举报