[JAVA] 获取当前日期所在周,月,年的第几天
JAVA 获取当前日期所在周,月,年的第几天
示例:
public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); //时间 Date nowDate = new Date(); calendar.setTime(nowDate); int weekDay = calendar.get(Calendar.DAY_OF_WEEK); int monthDay = calendar.get(Calendar.DAY_OF_MONTH); int yearDay = calendar.get(Calendar.DAY_OF_YEAR); weekDay = (weekDay==1?7:weekDay-1); System.out.println("当前时间是:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( nowDate )); System.out.println("当前时间是:本周的天:"+weekDay); System.out.println("当前时间是:本月的天:"+monthDay); System.out.println("当前时间是:本年的天:"+yearDay); }
输出效果:
补充说明:可以使用 SimpleDateFormat 类 格式化java的时间