每当我为程序提供不同的日期时,它总是给出错误日期的输出。它显示上一年 12 月份的日期。import java.text.*;import java.util.*;public class Sample { public static void main(String args[])throws Exception { Scanner s = new Scanner(System.in); System.out.println("Enter the details"); String str = s.nextLine(); String v[] = str.split(","); System.out.println(v[0]); System.out.println(v[1]); System.out.println(v[2]); try{ Date d1 = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss").parse(v[0]); Date d2 = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss").parse(v[1]); System.out.println(v[0]+" = "+ d1); System.out.println(v[1]+" = "+d2); } catch(Exception e) { e.printStackTrace(); } }}跑步:Enter the details06/05/2018 02:17:52,06/02/2015 03:15:33,Sourav06/05/2018 02:17:5206/02/2015 03:15:33Sourav06/05/2018 02:17:52 = Sun Dec 31 02:17:52 IST 201706/02/2015 03:15:33 = Sun Dec 28 03:15:33 IST 2014BUILD SUCCESSFUL (total time: 3 seconds)
2 回答
翻过高山走不出你
TA贡献1875条经验 获得超3个赞
我认为问题在于年份模式。请尝试以下操作:
Date d1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(v[0]);
添加回答
举报
0/150
提交
取消