我已经写了一个Java程序来提供折扣使用优惠券是自动生成的,我想设置它的到期时间,使优惠券在24小时后到期.并且下一个优惠券代码必须在24小时后生成.

public class Discount {

    public static void generateCouponCode() {
        String mycouponPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuffer stringBuffer = new StringBuffer(10);
        for (int i = 0; i < 10; i++) {
            int indexVal = (int)(mycouponPattern.length()*Math.random());
            stringBuffer.append(mycouponPattern.charAt(indexVal));
        }
        System.out.println("Coupon code generated successfully....");
        System.out.println("Your Generated coupon code is:"+stringBuffer.toString());
    }

    public static void main(String[] args) {
        Discount.generateCouponCode();
    }
}

推荐答案

Use the date-time API.
The below code uses class java.time.LocalDateTime to store the expiry date of the coupon. I changed class Discount to a record. I added a [static] member start for generating the expiry dates for the generated coupons. Simply call method plusHours to add 24 hours. Method main, in the below code, calls method generateCouponCode twice to demonstrate that the expiry date in the first generated coupon is the current time plus 24 hours and the second generated coupon's expiry date is 48 hours after the current time.

import java.time.LocalDateTime;

public record Discount(String coupon, LocalDateTime expires) {
    private static LocalDateTime  start = LocalDateTime.now();

    public static Discount generateCouponCode() {
        String mycouponPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuffer stringBuffer = new StringBuffer(10);
        for (int i = 0; i < 10; i++) {
            int indexVal = (int) (mycouponPattern.length() * Math.random());
            stringBuffer.append(mycouponPattern.charAt(indexVal));
        }
        System.out.println("Coupon code generated successfully....");
        start = start.plusHours(24L);
        Discount coupon = new Discount(stringBuffer.toString(), start);
        return coupon;
    }

    public static void main(String[] args) {
        System.out.println(Discount.generateCouponCode());
        System.out.println(Discount.generateCouponCode());
    }
}

以上代码的样例运行的输出:

Coupon code generated successfully....
Your Generated coupon code is:Q8HW5AIGHQ
Discount[coupon=Q8HW5AIGHQ, expires=2023-10-29T10:18:23.564755300]
Coupon code generated successfully....
Your Generated coupon code is:HEX57DHAE0
Discount[coupon=HEX57DHAE0, expires=2023-10-30T10:18:23.564755300]

EDIT

作为对OP comment的响应,以下代码将优惠券生成限制为仅在最后生成的优惠券过期后生成.

import java.time.LocalDateTime;

public record Discount(String coupon, LocalDateTime expires) {
    private static LocalDateTime  start = LocalDateTime.now();

    public static Discount generateCouponCode() {
        if (LocalDateTime.now().isAfter(start)) {
            String mycouponPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            StringBuffer stringBuffer = new StringBuffer(10);
            for (int i = 0; i < 10; i++) {
                int indexVal = (int) (mycouponPattern.length() * Math.random());
                stringBuffer.append(mycouponPattern.charAt(indexVal));
            }
            System.out.println("Coupon code generated successfully....");
            start = start.plusHours(24L);
            Discount coupon = new Discount(stringBuffer.toString(), start);
            return coupon;
        }
        else {
            throw new UnsupportedOperationException("Next coupon can be generated after: " + start);
        }
    }

    public static void main(String[] args) {
        System.out.println(Discount.generateCouponCode());
        System.out.println(Discount.generateCouponCode());
    }
}

运行上面的代码会产生以下输出:

Coupon code generated successfully....
Discount[coupon=TK7SWLHMBM, expires=2023-10-29T12:23:36.342781700]
Exception in thread "main" java.lang.UnsupportedOperationException: Next coupon can be generated after: 2023-10-29T12:23:36.342781700
    at basetest.Discount.generateCouponCode(Discount.java:22)
    at basetest.Discount.main(Discount.java:28)

Java相关问答推荐

Maven Google Sheets版本问题

OpenJDK、4K显示和文本质量

JsonPath在多个线程中返回错误的值

Spring boot:Bean和动态扩展器

工件部署期间出错[Tomcat 8.5.45]

使用标记时,场景大纲不在多个线程上运行

RichFaces 3.x-Spring Boot-迁移web.xml

使用Mockito进行的Junit测试失败

呈现文本和四舍五入矩形时出现的JavaFX窗格白色瑕疵

Chunk(Int)已弃用并标记为要删除

如何让DTO接受空字符串字段,但如果它们不为空,则应用JPA验证?

测试何时使用Mockito强制转换对象会导致ClassCastException

Kotlin Val是否提供了与Java最终版相同的可见性保证?

Java Telnet客户端重复的IAC符号

判断重复的两个二维表算法?

在实例化中指定泛型类型与不指定泛型类型之间的区别

根据应用程序 Select 的语言检索数据

为什么使用lo索引来解决二进制搜索问题不同于使用hi索引?

验证没有';t work on Hibernate Entity';s字段

Vaadin Flow:设置密码显示按钮属性