java时间日期格式总结(一)

来源:互联网 发布:opencv python之间联系 编辑:程序博客网 时间:2024/05/16 11:34
        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();        }

原创粉丝点击