直接上代码:
1 import java.util.*; 2 import java.text.SimpleDateFormat; 3 4 public class HelloWorld { 5 public static void main(String[] args) { 6 //可以方便地修改日期格式 7 Date now = new Date(); 8 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 9 String dateTime = dateFormat.format(now); 10 System.out.println(dateTime); 11 12 //可以对每个时间域单独修改 13 Calendar c = Calendar.getInstance(); 14 int year = c.get(Calendar.YEAR); 15 int month = c.get(Calendar.MONTH); //1月为0,2月为1,如此类推 16 int date = c.get(Calendar.DATE); 17 int hour = c.get(Calendar.HOUR_OF_DAY); 18 int minute = c.get(Calendar.MINUTE); 19 int second = c.get(Calendar.SECOND); 20 System.out.println(year + "/" + (month + 1) + "/" + date + " " + hour + ":" + minute + ":" + second); 21 } 22 }
输出:
2019/03/25 17:19:34 2019/3/25 17:19:34
原文地址:https://www.cnblogs.com/lipohong/p/10594985.html
时间: 2024-10-08 05:24:40