java時間日期格式總結(一)

        try {
            String sdate = "2017-10-12 14:33:22:123";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
            //年YYYY變2017-01-01,
            //月MM=M,MMM變十月大寫月份,
            //日D變2017-01-12取1月份該日,
            //小時,24小時HH,12小時hh,
            //分小寫
            //秒小寫
            //毫秒大寫
            Date d = sdf.parse(sdate);
            String sd = sdf.format(d);
            System.out.println(sd);
            Calendar cal = Calendar.getInstance();
            //當前年
            int year = cal.get(Calendar.YEAR);
            //當前月,從0開始
            int month = cal.get(Calendar.MONTH) + 1;
            //當前月的第幾天,DAY_OF_MONTH與DATE等價
            int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
            int date = cal.get(Calendar.DATE);
            //當前時鐘,24小時制
            int hour24 = cal.get(Calendar.HOUR_OF_DAY);
            //當前時間,12小時制
            int hour12 = cal.get(Calendar.HOUR);
            //當前分鐘
            int minute = cal.get(Calendar.MINUTE);
            //當前秒
            int second = cal.get(Calendar.SECOND);
            //星期幾 1-7表示日-六
            int day_of_week = cal.get(Calendar.DAY_OF_WEEK);
            //上下午 0-上午 1-下午
            int ampm = cal.get(Calendar.AM_PM);
            //當前年的第幾周
            int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
            //當前月的第幾周
            //int day_of_week_in_month = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH); //某月中第幾周,
            //按這個月1號算,1號起就是第1周,8號起就是第2周。以月份天數爲標準 
            //int week_of_month = calendar.get(Calendar.WEEK_OF_MONTH);//日曆式的第幾周
            //(例如今天是8-21,是八月的第四周)
            int week_of_month = cal.get(Calendar.WEEK_OF_MONTH);
            int day_of_week_in_month = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
            //當前年的第幾天
            int day_of_year = cal.get(Calendar.DAY_OF_YEAR);
            System.out.println(year);
            System.out.println(month);
            System.out.println(day_of_month);
            System.out.println(date);
            System.out.println(hour24);
            System.out.println(hour12);
            System.out.println(minute);
            System.out.println(second);
            System.out.println(day_of_week);
            System.out.println(ampm);
            System.out.println(week_of_year);
            System.out.println(week_of_month);
            System.out.println(day_of_week_in_month);
            System.out.println(day_of_year);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章