本文共 2596 字,大约阅读时间需要 8 分钟。
JDK:1.8
官方API文档:
参考:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();System.out.println(sdf.format(date));
参考:
Calendar calendar = Calendar.getInstance();int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH) + 1;int day = calendar.get(Calendar.DATE);// int hour = calendar.get(Calendar.HOUR); // 12小时制int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制int minute = calendar.get(Calendar.MINUTE);int second = calendar.get(Calendar.SECOND);System.out.println(year +"-"+ month +"-"+ day +" "+ hour +":"+ minute +":"+ second);// orSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(calendar.getTime()));
JDK:1.8+
参考:
DateTimeFormatter dateDTF = DateTimeFormatter.ofPattern("yyyy-MM-dd");LocalDate localDate = LocalDate.now();DateTimeFormatter timeDTF = DateTimeFormatter.ofPattern("HH:mm:ss");LocalTime localTime = LocalTime.now();System.out.println(localDate.format(dateDTF) +" "+ localTime.format(timeDTF));// orDateTimeFormatter datetimeDTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDateTime.format(datetimeDTF));
参考:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(System.currentTimeMillis());System.out.println(sdf.format(date));
参考:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();Timestamp timestamp = new Timestamp(date.getTime());System.out.println(sdf.format(timestamp));// ortimestamp = new Timestamp(System.currentTimeMillis());date = new Date(timestamp.getTime());System.out.println(sdf.format(date));
参考:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();java.sql.Date sqlDate = new java.sql.Date(date.getTime());System.out.println(sdf.format(sqlDate));// orsqlDate = new java.sql.Date(System.currentTimeMillis());date = new Date(sqlDate.getTime());System.out.println(sdf.format(date));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");Calendar calendar = Calendar.getInstance();calendar.setTime(new Date()); // 设置当前时间for(int i=0; i< 24; i++){ String time = sdf.format(calendar.getTime()); System.out.println(time); // 如果 -1 则为是过去24小时的时间 calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 1);}
转载地址:http://swnws.baihongyu.com/