简体   繁体   English

我面临 dateFormat.parse(string) 的问题?

[英]Issue i am facing with dateFormat.parse(string)?

I have below code snippet我有以下代码片段

SimpleDateFormat dateFormat = new SimpleDateFormat(
      "yyyy-MM-dd hh:mm:ss.SSS");
 String processedContentDate="2012-04-10 12:53:28.033";   
 java.util.Date parsedDate = dateFormat.parse(processedContentDate);
java.sql.Timestamp timestamp = new java.sql.Timestamp(
 parsedDate.getTime());

I get parsed date as Tue Apr 10 00:53:28 IST 2012 and timestamp as 2012-04-10 00:53:28.033.我得到的解析日期为 2012 年 4 月 10 日星期二 00:53:28 IST,时间戳为 2012-04-10 00:53:28.033。 i want to get the time exactly as 12:53:28.033(as in my original string) not 00:53:28.033.我想让时间准确地为 12:53:28.033(如我原来的字符串),而不是 00:53:28.033。 Not getting why 12:53:28 is getting converted to 00:53:28.不明白为什么 12:53:28 会转换为 00:53:28。 what should I do to get 12:53:28?我应该怎么做才能得到 12:53:28?

EDIT: After getting the response, I tried this small programme where current time is 14:34:38.899 but at both lines ie at line 1 and line 2, I got below parsed date 2012-04-10 14:34:38.899 As per reply I should have got 02:34:38.899 at line 1 as date format is yyyy-MM-dd hh:mm:ss.SSS")编辑:收到回复后,我尝试了这个当前时间为 14:34:38.899 的小程序,但在两行,即第 1 行和第 2 行,我得到的解析日期低于 2012-04-10 14:34:38.899 按照回复 我应该在第 1 行得到 02:34:38.899,因为日期格式是yyyy-MM-dd hh:mm:ss.SSS")

    java.util.Date date= new java.util.Date();
    String strDate=date.toString();
    java.util.Date parsedDate;
    java.util.Date parsedDate2;
     SimpleDateFormat dateFormat = new SimpleDateFormat(
     "yyyy-MM-dd hh:mm:ss.SSS");// line 1
     SimpleDateFormat dateFormat2 = new SimpleDateFormat(
     "yyyy-MM-dd HH:mm:ss.SSS");//line 2
     try {
         java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
          strDate=timestamp.toString();


            parsedDate = dateFormat.parse(strDate);//line1
            parsedDate2 = dateFormat2.parse(strDate);//line2

Define your dateFormat like that像这样定义你的dateFormat

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

HH instead of hh . HH而不是hh See SimpleDateFormat请参见简单日期格式

Your date format must be yyyy-MM-dd HH:mm:ss.SSS .您的日期格式必须是yyyy-MM-dd HH:mm:ss.SSS

hh is hours in am/pm, while HH is hours in a day (that's where you mistake is). hh是上午/下午的小时数,而HH是一天中的小时数(这就是您的错误所在)。 See SimpleDateFormat .请参阅SimpleDateFormat

As per definition of Date.toString() and Timestamp.toString , the .toString() output is always using a 24-hour clock.根据Date.toString()Timestamp.toString的定义, .toString() output 始终使用 24 小时制。 If you want to show the time using AM/PM, you should use the dateformatter to print the date.如果你想使用 AM/PM 显示时间,你应该使用 dateformatter 来打印日期。 As you are using the same date/time as a source for both ( strDate will use 14:34), when you parse the date, the SimpleDateFormat using the 12-hour clock is "lenient" and allows parsing of 14 as an hour.由于您使用相同的日期/时间作为两者的来源( strDate将使用 14:34),因此当您解析日期时,使用 12 小时制的SimpleDateFormat是“宽松的”并且允许将 14 解析为一个小时。

If you set如果你设置

dateFormat.setLenient(false);

you'll probably find that the dateFormat.parse(strDate) will fail.您可能会发现dateFormat.parse(strDate)会失败。

To print dates, I would never rely on toString , but always use a formatter.要打印日期,我永远不会依赖toString ,而是始终使用格式化程序。

System.out.println(dateFormat.format(parsedDate)); // should show ...02:36...
System.out.println(dateFormat2.format(parsedDate)); // should show ...14:36...
System.out.println(dateFormat.format(parsedDate2)); // should show ...02:36...
System.out.println(dateFormat2.format(parsedDate2)); // should show ...14:36...

Try below code:试试下面的代码:

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = fmt.parse("yourdate");日期 date = fmt.parse("yourdate"); SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy hh:mm a");String myDate = fmtOut.format(date); SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy hh:mm a");String myDate = fmtOut.format(date);

If yourdate is 2016-06-10 12:06:43, then output will be 10-06-2016 12:06 pm.如果你的日期是 2016-06-10 12:06:43,那么 output 将是 10-06-2016 12:06 pm。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM