代码之家  ›  专栏  ›  技术社区  ›  Carlos Moretti

如何在Java日期API中返回时间

  •  0
  • Carlos Moretti  · 技术社区  · 10 月前

    如何使用Eclipse/SpringBoot强制新的Date()和您的变体(如LocalTime和其他API)在整个应用程序中返回D-1值?改变我的电脑时钟工作吗?

    非常感谢。

    尝试使用豆子,但效果不佳

    2 回复  |  直到 10 月前
        1
  •  0
  •   Dodge_X    10 月前

    如果你更改电脑日期,它就会工作。Spring Boot应用程序通常依赖于系统时间进行日期和时间操作。因此,如果您更改计算机日期,它将工作。您可以使用

    System.out.println(LocalDateTime.now());
    

    您也可以更改应用程序时区,但它不能加-24小时(-12小时是可以的),这取决于您的位置。

    @springBootApplication
    public class Application {
        public static void main(string[] args) {
            TimeZone.setDefault(TimeZone.getTimeZone(XXXX));
            SpringApplication.run(Application.class, args);
        }
    }
    
    
        2
  •  0
  •   Athira Sabu    10 月前

    以下是使用Spring Boot实现此目的的策略:

    1. 摘要日期/时间检索 创建一个接口来抽象当前日期/时间的检索:
    public interface DateTimeProvider {
        Date getCurrentDate();
        LocalDate getCurrentLocalDate();
        LocalTime getCurrentLocalTime();
    }
    
    1. 实现接口 创建一个返回实际当前日期/时间的默认实现:
    import java.time.LocalDate;
    import java.time.LocalTime;
    import java.util.Date;
    
    public class DefaultDateTimeProvider implements DateTimeProvider {
    
        @Override
        public Date getCurrentDate() {
            return new Date();
        }
    
        @Override
        public LocalDate getCurrentLocalDate() {
            return LocalDate.now();
        }
    
        @Override
        public LocalTime getCurrentLocalTime() {
            return LocalTime.now();
        }
    }
    
    1. 创建D-1实施 创建一个返回“D-1”的DateTimeProvider实现:
    import java.time.LocalDate;
    import java.time.LocalTime;
    import java.util.Date;
    import java.util.Calendar;
    
    public class DMinusOneDateTimeProvider implements DateTimeProvider {
    
        @Override
        public Date getCurrentDate() {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            return calendar.getTime();
        }
    
        @Override
        public LocalDate getCurrentLocalDate() {
            return LocalDate.now().minusDays(1);
        }
    
        @Override
        public LocalTime getCurrentLocalTime() {
            return LocalTime.now(); // Assuming you want to keep the time unchanged
        }
    }
    
    
    1. 在Spring Boot中配置Bean 通过将其设置为Spring Bean来配置要使用的DateTimeProvider实现。您可以使用配置文件或配置属性在默认和“D-1”提供程序之间切换。

    例如,您可以在配置类中定义bean:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    
    @Configuration
    public class DateTimeConfig {
    
        @Bean
        @Profile("default") // Active by default
        public DateTimeProvider dateTimeProvider() {
            return new DefaultDateTimeProvider();
        }
    
        @Bean
        @Profile("dminus1") // Activate this profile to use D-1
        public DateTimeProvider dMinusOneDateTimeProvider() {
            return new DMinusOneDateTimeProvider();
        }
    }
    
    1. 在应用程序中使用DateTimeProvider 在需要使用日期/时间值的地方注入DateTimeProvider:
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class MyService {
    
        private final DateTimeProvider dateTimeProvider;
    
        @Autowired
        public MyService(DateTimeProvider dateTimeProvider) {
            this.dateTimeProvider = dateTimeProvider;
        }
    
        public void doSomething() {
            Date currentDate = dateTimeProvider.getCurrentDate();
            LocalDate currentLocalDate = dateTimeProvider.getCurrentLocalDate();
            LocalTime currentLocalTime = dateTimeProvider.getCurrentLocalTime();
            
            // Use these values in your business logic
        }
    }
    
    1. 使用特定配置文件运行 启动Spring Boot应用程序时,可以指定在不同日期/时间提供程序之间切换的活动配置文件:
    # To use the default date/time provider
    java -jar myapp.jar --spring.profiles.active=default
    
    # To use the D-1 date/time provider
    java -jar myapp.jar --spring.profiles.active=dminus1