Calendar now = Calendar.getInstance(); System.out.println("年: " + now.get(Calendar.YEAR)); System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + ""); System.out.println("月11: " + (now.get(Calendar.MONTH) + 1)); System.out.println("月1122: " + now.get(Calendar.MONTH + 1)); System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH)); System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY)); System.out.println("分: " + now.get(Calendar.MINUTE)); System.out.println("秒: " + now.get(Calendar.SECOND)); System.out.println("当前时间毫秒数:" + now.getTimeInMillis()); System.out.println(now.getTime()); Date d = new Date(); System.out.println(d); SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); SimpleDateFormat sdf1 = new SimpleDateFormat("MM"); SimpleDateFormat sdf2 = new SimpleDateFormat("dd"); SimpleDateFormat sdf3 = new SimpleDateFormat("HH"); String dateNowStr = sdf.format(d); String dateNowStr1 = sdf1.format(d); String dateNowStr2 = sdf2.format(d); String dateNowStr3 = sdf3.format(d); System.out.println("格式化后的日期:" + dateNowStr); System.out.println("格式化后的日期:" + dateNowStr1); System.out.println("格式化后的日期:" + dateNowStr2); System.out.println("格式化后的日期:" + dateNowStr3);
Java 中快速获取年,月,日,时的方法
最新推荐文章于 2024-04-25 11:07:58 发布