【问题标题】:Converting String to Date error将字符串转换为日期错误
【发布时间】:2026-02-11 14:00:02
【问题描述】:

我有这样的日期字符串值:Fri, 27-Sep-2013 08:29:59 GMT 我需要将其转换为日期格式 我试过这样:

private Date modifyDateLayout(String inputDate) {
        DateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        format.setTimeZone (TimeZone.getTimeZone ("GMT"));
        Date d = null;
        try {
        d = format.parse(inputDate);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return d;
    }

但它不起作用,并且 e=null 我哪里错了? 感谢您的帮助

【问题讨论】:

  • 1) 尝试 DateFormat 格式 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS'Z'");没有'T'并在设置日期模板之前设置时区。 2)可能传入的字符串无效可能是这个java67.blogspot.com/2013/01/…帮助你
  • 第一个解决方案也不起作用...字符串有效,我从调试中复制了有问题的字符串

标签: java android datetime


【解决方案1】:

试试这个效果很好

private Date modifyDateLayout(String inputDate) {
        SimpleDateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss");
        format.setTimeZone(TimeZone.getDefault());
        Date d = null;
        try {
            d = format.parse(inputDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

致电

modifyDateLayout("Fri, 27-Sep-2013 08:29:59 GMT");

【讨论】:

  • 谢谢!我不知道原因,但钢返回 null。即使是您回答的完整副本,我也无法理解...感谢您的帮助!
【解决方案2】:

格式应为EEE, dd-MMM-yyyy hh:mm:ss ZZZ

【讨论】:

    【解决方案3】:

    试试这个

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    System.out.println(format.format(now));
    String s = format.format(now);
    String result = s.substring(0, 26) + ":" + s.substring(27);
    System.out.println("Result: "+result);
    

    【讨论】:

      【解决方案4】:

      这应该是您的格式。 EEE, dd-MMM-yyyy hh:mm:ss ZZZ

      这里是working output

      【讨论】:

      • 我无法理解...我复制了你所有的代码(即使使用字符串 const),但 Steel 不起作用!
      • 在捕获 e = null! :o
      【解决方案5】:

      试试这个....

      private Date modifyDateLayout(String inputDate) {
          DateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
          format.setTimeZone(TimeZone.getTimeZone("GMT"));
          Date d = null;
          try {
              d = format.parse(inputDate);
          } catch (ParseException e) {
              e.printStackTrace(); // To change body of catch statement use File |
                                      // Settings | File Templates.
          }
          return d;
      }
      

      【讨论】:

        【解决方案6】:

        这是有效的。只需根据格林威治标准时间设置您的时间,它就会为您提供当地时间。

        import java.text.DateFormat;
        import java.text.ParseException;
        import java.text.SimpleDateFormat;
        import java.util.Date;
        import java.util.TimeZone;
        
        
        public class Time {
            private Date modifyDateLayout(String inputDate) {
                DateFormat format = new SimpleDateFormat ("EEE, dd-MMM-yyyy hh:mm:ss zzz");
                format.setTimeZone (TimeZone.getTimeZone ("GMT"));
        
                Date d = null;
                try {
                d = format.parse(inputDate);
                System.out.println(d);
                } catch (ParseException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                return d;
            }
            public static void main(String[] args) {
                //new Time().modifyDateLayout("Fri, 27-Sep-2013 08:29:59 GMT");
                new Time().modifyDateLayout("Fri, 17-Apr-2015 15:45:59 GMT");
            }
        }
        

        【讨论】:

        • @Vitality 您在第二行中犯了错误 DateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");