gpt4 book ai didi

java - 操作日期的静态方法。多线程问题

转载 作者:行者123 更新时间:2023-11-30 06:32:40 26 4
gpt4 key购买 nike

friend ,

我正在提供我的生产代码的修改后的摘录。

运行以下代码时,尽管我正在通过传递 auditdate1 执行 getDateEndOfDay,但我收到“TestDateProblem:getYear 方法问题”。

我无法真正解决这个问题,因为日期是正确的,我可以在我的生产代码的 catch block 中的记录器中看到它。我非常需要你的帮助。

getDateEndOfDay 方法从许多公共(public)方法调用,并且多个线程可能同时调用这些静态方法。

package test;

import java.util.Arrays;

import java.util.Date;

import java.util.GregorianCalendar;


public class TestDateProblem {

public static void main(String args[]) {
/*
* This is the date format that is mostly used. Logger shows this kind
* of date during Exception.
*/
String auditdate1 = "2011-12-27";
// Rarely sent this way.
String auditdate2 = "27-12-2011";
/*
* We don't send this way but I'm sure the problem occurs if the date goes this
* way. As far as the inputs are concerned, it doesn't go like this.
*/
String auditdate3 = "27-2011-12";

try {

System.out.println("Result1:" + getDateEndOfDay(auditdate1));

System.out.println("Result2:" + getDateEndOfDay(auditdate2));
// simulating problem?
System.out.println("Result3:" + getDateEndOfDay(auditdate3));
} catch (Exception e) {
System.out.println("auditdate1:" + auditdate1);
}
}

/*
* This getDateEndOfDay(.) method is called from many public methods and it
* may be possible that multiple threads are calling these static methods at
* the same time.
*/
public static Date getDateEndOfDay(String dateparam) throws Exception {
String separator = "/";
dateparam = dateparam.replace("-", separator);
String[] strP = dateparam.split(separator);
Integer year = getYear(strP);
Integer month = getMonth(strP);
Integer day = getDay(strP);
GregorianCalendar cal = new GregorianCalendar(year, month - 1, day, 23,
59, 00);

return cal.getTime();
}

private static Integer getYear(String[] dateComponents) throws Exception {
if (dateComponents.length != 3) {
return 1900;
}
System.out
.println("dateComponents::" + Arrays.toString(dateComponents));

Integer val1 = Integer
.valueOf(dateComponents[0].startsWith("0") ? dateComponents[0]
.substring(1) : dateComponents[0]);
Integer val2 = Integer
.valueOf(dateComponents[2].startsWith("0") ? dateComponents[2]
.substring(1) : dateComponents[2]);
if (val1 > 1900) {
return val1;
} else if (val2 > 1900) {
return val2;
} else {
// Original code throws exception instead of printing to console.
System.out.println("TestDateProblem: Problem with getYear method.");
throw new Exception();
}
}

private static Integer getDay(String[] dateComponents) {
if (dateComponents.length != 3) {
return -1;
}

Integer val1 = Integer
.valueOf(dateComponents[0].startsWith("0") ? dateComponents[1]
.substring(1) : dateComponents[0]);
Integer val2 = Integer
.valueOf(dateComponents[2].startsWith("0") ? dateComponents[1]
.substring(1) : dateComponents[2]);

if (val1 <= 31) {
return val1;
} else if (val2 <= 31) {
return val2;
} else {
System.out.println("TestDateProblem: Problem with getDay method.");
return 0;
}
}

private static Integer getMonth(String[] dateComponents) {
if (dateComponents.length != 3) {
return 0;
}

Integer val1 = Integer
.valueOf(dateComponents[1].startsWith("0") ? dateComponents[1]
.substring(1) : dateComponents[1]);

if (val1 <= 12 && val1 >= 1) {
return val1;
} else {
System.out
.println("TestDateProblem:: Problem with getMonth method");
return 0;
}
}
}

最佳答案

虽然代码不必要地冗长,但也相当清晰。您遇到的问题是它试图确定日期是 yyyy/mm/dd 格式还是 dd/mm/yyyy 格式,但是如果您有时给它像 yy/mm/dd 格式,它无法计算出什么它是并给出了您看到的错误。

使用调试器单步执行代码将证实这一点。

我会再次阅读这条评论,因为它解释了问题

* We don't send this way but the problem occurs if the date goes this
* way.

你给它一个无效的格式化日期,它拒绝它是正确的,因为它不知道如何处理它。

关于java - 操作日期的静态方法。多线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8643066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com