java取当天日期_Java09-获取当前日期的3种方式

packagedoudou;importjava.util.Date;importjava.text.SimpleDateFormat;importjava.time.LocalDate;importjava.time.LocalDateTime;importjava.time.LocalTime;importjava.time.format.DateTimeFormatter;importjava.util.Calendar;public classtest_date {public static voidmain(String[] args) {

System.out.println("============方式一:Date==========================");

Date a= newDate();int current_year = a.getYear() + 1900;int current_month = a.getMonth() + 1;

System.out.println("当前年:" +current_year);

System.out.println("当前月:" + current_month);//Expired 月份 从0开始

System.out.println("当前日:" + a.getDate());//Expired 日

System.out.println("当前日期及时间: " + a.toString()); //当前日期及时间

System.out.println(a.getClass());//通过对象a调用getClass()方法返回Class

System.out.println("============方式二:SimpleDateFormat和DateTimeFormatter==========================");//格式化时间(SimpleDateFormat和DateTimeFormatter)

System.out.println("!!!!SimpleDateFormat:线程不安全!!!!");

Date date= newDate();

String strDateFormat= "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat sdf= new SimpleDateFormat("当前日期及时间: " +strDateFormat);

System.out.println(sdf.format(date));

String strDateFormat1= "yyyy-MM-dd";

SimpleDateFormat sdf1= new SimpleDateFormat("当前日期: " +strDateFormat1);

System.out.println(sdf1.format(date));

String strDateFormat2= "HH:mm:ss";

SimpleDateFormat sdf2= new SimpleDateFormat("当前时间: " +strDateFormat2);

System.out.println(sdf2.format(date));

System.out.println("!!!!DateTimeFormatter-LocalDateTime:线程安全!!!!");

DateTimeFormatter dtf= DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

System.out.println("当前日期及时间: " +dtf.format(LocalDateTime.now()));

LocalDate localDate=LocalDate.now();

System.out.println("当前日期:" +localDate.toString());

LocalTime localTime= LocalTime.now().withNano(0);//去掉毫秒

System.out.println("当前时间:" +localTime.toString());

System.out.println("==============方式三:Calendar====================");

Calendar cal=Calendar.getInstance();

Date a1=cal.getTime();

System.out.println("当前日期及时间:" +a1);int day =cal.get(Calendar.DATE);int month = cal.get(Calendar.MONTH) + 1;int year =cal.get(Calendar.YEAR);int hour =cal.get(Calendar.HOUR);int minute =cal.get(Calendar.MINUTE);int second =cal.get(Calendar.SECOND);

System.out.println("Month:" +month);

System.out.println("Day:" +day);

System.out.println("year:" +year);

System.out.println("WEEK_OF_YEAR:" +cal.get(cal.WEEK_OF_YEAR));

System.out.println("WEEK_OF_MONTH:" +cal.get(cal.WEEK_OF_MONTH));

System.out.println("DAY_OF_WEEK:" + cal.get(cal.DAY_OF_WEEK));//从周日开始算

System.out.println("现在是" + year + "年" + month + "月" + day + "日");

System.out.println("现在是" + hour + "时" + minute + "分" + second + "秒");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值