Java数据比较
我使用下面的代码查看我的输入日期(格式Java数据比较,java,date,date-comparison,Java,Date,Date Comparison,我使用下面的代码查看我的输入日期(格式mm/dd/yyyy HH:mm:ss)是否在startDate和endDate范围内 我在这个逻辑中使用compareTo()。但是使用格式mm/dd/yyyy,它只比较月份,并以“MYLOGIC Method”打印输出。但是我需要比较年份,以查看输入日期是否在startDate和endDate范围内 public class DateLogicNew { public static void main(String[] args) {
mm/dd/yyyy HH:mm:ss
)是否在startDate
和endDate
范围内
我在这个逻辑中使用compareTo()
。但是使用格式mm/dd/yyyy
,它只比较月份,并以“MYLOGIC Method”
打印输出。但是我需要比较年份,以查看输入日期是否在startDate
和endDate
范围内
public class DateLogicNew {
public static void main(String[] args) {
String startDate[] = new String[1];
String endDate[] = new String[1];
startDate[0] = "01/01/0600 00:00:00";
endDate[0] = "11/27/3337 00:00:00";
String inputArr[] = { "05/01/0500 01:00:00", "11/27/3337 00:00:00",
"05/05/0700 00:00:00", "11/27/2337 00:00:00",
"06/05/4000 00:00:00" };
String protectedArr[] = new String[inputArr.length];
int temp[] = new int[inputArr.length];
System.out.println(inputArr.length);
System.out.println("Length of the inputArr: " + inputArr.length);
// System.out.println("");
for (int i = 0; i < inputArr.length; i++) {
if (inputArr[i]
.matches("^([0-1][0-9])/([0-3][0-9])/([0-9]{4})(?:( [0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$")) {
System.out.println("Inside if loop");
if (inputArr[i].compareTo(startDate[0]) > 0
&& inputArr[i].compareTo(endDate[0]) < 0) {
System.out.println("inside the compare condition");
temp[i] = 1;
protectedArr[i] = inputArr[i];
System.out
.println("Values of the inputArr in MYLOGIC method : "
+ protectedArr[i]);
}
} else {
temp[i] = 0;
}
}
System.out.println("");
for (int i = 0; i < inputArr.length; i++) {
if (temp[i] == 1) {
inputArr[i] = protectedArr[i];
}
System.out
.println("Final Value to the output port: " + inputArr[i]);
}
}
}
公共类DateLogicNew{
公共静态void main(字符串[]args){
字符串startDate[]=新字符串[1];
字符串结束日期[]=新字符串[1];
起始日期[0]=“01/01/0600:00:00”;
endDate[0]=“11/27/3337 00:00:00”;
字符串inputArr[]={“05/01/0500 01:00:00”,“11/27/3337 00:00:00”,
"05/05/0700 00:00:00", "11/27/2337 00:00:00",
"06/05/4000 00:00:00" };
String protectedArr[]=新字符串[inputArr.length];
int temp[]=新的int[inputArr.length];
System.out.println(inputArr.length);
System.out.println(“输入的长度:“+inputArr.Length”);
//System.out.println(“”);
for(int i=0;i0
&&inputArr[i]。比较(结束日期[0])<0){
System.out.println(“在比较条件内”);
温度[i]=1;
protectedArr[i]=输入Arr[i];
系统输出
.println(“MYLOGIC方法中inputArr的值:”
+protectedArr[i]);
}
}否则{
温度[i]=0;
}
}
System.out.println(“”);
for(int i=0;i
java.time
解析为日期时间对象。正则表达式太过分了
现代方法使用java.time类,而不是旧的遗留日期时间类
String input = "05/01/0500 01:00:00" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
使用isBefore、isAfter、isEqual等方法进行比较
if( ( ! x.isBefore( start ) ) && x.isBefore( stop ) ) { … }
我自己想出了解决问题的办法。在比较日期之前,请检查下面的代码以查看逻辑 字符串startDate=“0600/01/01 00:00:00”; 字符串endDate=“3337/11/27 00:00:00”
试试看{
for(int i=0;i0
&&newFormattedInput.compareTo(endDate)<0){
温度[i]=1;
protectedArr[i]=输入Arr[i];
系统输出
.println(“保护方法中的inputArr值:”
+protectedArr[i]);
}否则{
温度[i]=0;
}
}
}
}捕获(解析异常){
e、 printStackTrace();
}
您需要更清楚地描述您的问题-发生了什么、您预期会发生什么、它们之间的区别、一些示例输入和输出等。您还可能会发现使用内置的日期解析和处理库更容易。为什么不使用实际的日期
实例进行比较?比较基于字符串的日期肯定很棘手。字符串比较是在逐个字符的基础上进行的,因此10/31/9999
仍然比11/27/3337
小,因为索引1(0和1)处的字符是独立于其他字符进行比较的。你真的关心500年的凌晨1点吗?还是这里发生了什么事?这个例子数据很奇怪。
try {
for (int i = 0; i < inputArr.length; i++) {
String newDateInput = inputArr[i];
// System.out.println("NewInput:" + newInput);
DateFormat parser = new SimpleDateFormat("MM/dd/yyyy");
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date convertedDate = parser.parse(newDateInput);
String newFormattedInput = formatter.format(convertedDate);
// System.out.println("newFormattedInput: " +
// newFormattedInput);
if (newFormattedInput
.matches("^([0-9]{4})/([0-1][0-9])/([0-3][0-9])(?:( [0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$")) {
if (newFormattedInput.compareTo(startDate) > 0
&& newFormattedInput.compareTo(endDate) < 0) {
temp[i] = 1;
protectedArr[i] = inputArr[i];
System.out
.println("Values of the inputArr in PROTECT method : "
+ protectedArr[i]);
} else {
temp[i] = 0;
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}