package com.ruoyi.zhushi.util; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; public class DateUtils { public static String getDate() { LocalDate today = LocalDate.now(); // 定义日期格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 格式化日期为字符串 String formattedDate = today.format(formatter); return formattedDate; } public static String getTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date currentDate = new Date(); return sdf.format(currentDate); } //获取不同时区的时间 public static String getCurrentTimeByZone(String timezone) { try { // 创建指定时区的时间对象 ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of(timezone)); // 格式化输出 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return zonedDateTime.format(formatter); } catch (Exception e) { return "无效的时区:" + timezone; } } }