首页 > 代码库 > Java:日期类Date与Calendar

Java:日期类Date与Calendar

怎么获取系统系统时间,通过java?

Date now=   new Date();SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSSSS");String nowFormate=   dateFormat.format(now);System.out.println(nowFormate);

输出结果

2017-02-16 20:56:12.000000278

如何获取当前系统是星期?

方法一、

    String weekOfDay= getWeekOfDay(now);    System.out.println(weekOfDay);    public static String getWeekOfDay(Date date){        String[] weekDays=new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};        Calendar calendar= Calendar.getInstance();        calendar.setTime(date);       int weekIndex= calendar.get(Calendar.DAY_OF_WEEK)-1;        if(weekIndex<0)            weekIndex=0;        return weekDays[weekIndex];    }

方法二、

        SimpleDateFormat   dateFormat2=new SimpleDateFormat("EEEE");        String nowFormate2=   dateFormat2.format(now);        System.out.println(nowFormate2);

 

Calendar获取当前系统日期的年、月、日

        Calendar calendar= Calendar.getInstance();        calendar.setTime(now);        int year= calendar.get(Calendar.YEAR);        int month= calendar.get(Calendar.MONTH)+1;        int day= calendar.get(Calendar.DAY_OF_MONTH);        System.out.println(year+"-"+month+"-"+day);

 

Java:日期类Date与Calendar