Java将String转换为date,然后转换为Compare [英] Java convert String to date and then Comparison

查看:115
本文介绍了Java将String转换为date,然后转换为Compare的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D字符串数组

  static String [] [] TaskStorage = {
,02/07/13},
{任务2,02/08/13},
{任务3,02/09/13},
{任务4,02/10/13},
{任务5,02/11/13},
{任务6 03/13},
{Task 7,02/04/13},
{Task 8,02/05/13},
{任务9,02/06/13},
{任务10,03/01/13},
{任务11,03/02/13} ,
{Task 12,03/03/13},
};



我想打印所有TaskStorage [0] [i]其中TaskStorage [i] [1]是02/07和02/08。
考虑到今天是02/04。今天的日期可以更改为02/28。
todayDate将基于代码的执行时间。

基本上是两天前的打印任务。



这是我尝试过:

  SimpleDateFormat dateFormat = new SimpleDateFormat(mm / dd / yy); 
TaskDate = dateFormat.parse(LinkStorage [i] [1]);
if(todayDate.after(TaskDate)){
System.out.println();
}


解决方案

SimpleDateFormat 将字符串解析为 Date 对象,然后通过 Date API进行比较。



修改问题:


以下是我试过的。

  SimpleDateFormat dateFormat = new SimpleDateFormat(mm / dd / yy); 
TaskDate = dateFormat.parse(LinkStorage [i] [1]);
if(todayDate.after(TaskDate)){
System.out.println()
}


问题是你的格式字符串,你问分钟,而不是。更改mm / dd / yyMM / dd / yy,你应该有更好的运气。 / p>

I have a 2D String array

static String[][] TaskStorage = {
{"Task 1","02/07/13"},  
{"Task 2","02/08/13"},  
{"Task 3","02/09/13"},  
{"Task 4","02/10/13"},  
{"Task 5","02/11/13"},  
{"Task 6","02/03/13"},  
{"Task 7","02/04/13"},  
{"Task 8","02/05/13"},  
{"Task 9","02/06/13"},  
{"Task 10","03/01/13"}, 
{"Task 11","03/02/13"}, 
{"Task 12","03/03/13"}, 
};

I would like to print all TaskStorage[0][i] where TaskStorage[i][1] is 02/07 and 02/08. Taking into consideration that todayDate is 02/04. Today date can change to 02/28. todayDate will be based on the execution time of the code.
Basically printing tasks that are two days ahead.

This is what I've tried:

SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yy");
TaskDate = dateFormat.parse(LinkStorage[i][1]);
if (todayDate.after(TaskDate)) {
    System.out.println("");
}

解决方案

One approach would be to use SimpleDateFormat to parse the strings to Date objects and then do the comparisons through the Date API.

Edit for change to Question:

Below is what I have tried.

SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yy");  
TaskDate = dateFormat.parse(LinkStorage[i][1]);  
if (todayDate.after(TaskDate)) {  
    System.out.println("")  
}  

Unfortunately not getting desired output

The problem is with your format string, you are asking for Minutes and not Months. Change "mm/dd/yy" to "MM/dd/yy" and you should have better luck.

这篇关于Java将String转换为date,然后转换为Compare的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆