【问题标题】:Date formatting in AndroidAndroid中的日期格式
【发布时间】:2014-01-19 21:40:11
【问题描述】:

我有一个问题,我想将字符串解析为“2013 年 11 月 15 日”格式的日期,但我无法在 SimpleDateFormat 类中使用 MMMM D、YYYY 来执行此操作。请提出任何解决方案。

代码:

SimpleDateFormat formatter = new SimpleDateFormat("MMMM DD, yyyy");
try {
    Date publishedDate = formatter.parse(pictureDirectory.replace(str, ""));
    hashMap.put(publishedDate, getImageFromSdCard(picturePath));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

它总是返回“Fri Nov 15 00:00:00 IST 2013”​​。

【问题讨论】:

  • 这仅显示您如何解析日期。你是怎么打印出来的?
  • 问题不是关于打印,而是没有返回我想要的结果。请就此提出任何解决方案。
  • 好吧,如果它“返回”某些东西并且您说它返回“Fri Nov 15 00:00:00 IST 2013”​​,那么您可以通过某种方式将结果显示在某处,但问题并没有说明如何.
  • 哦,我正在调试代码并检查特定变量以检查它返回的内容。
  • 调试器显示Date.toString() 的输出。它没有格式化为任何特定的内容。

标签: android simpledateformat


【解决方案1】:

你可以这样做

SimpleDateFormat orignalFormat= new SimpleDateFormat("MMMMMMMMM dd, yyyy"); //November 15, 2013
            Date date = null;
            try {
                    date = origFormat.parse(apkgalaxy.getPosted_on());              
            } catch (ParseException e) {
                e.printStackTrace();
            }

并通过以下代码将日期设置为所需格式

SimpleDateFormat newFormat_date= new SimpleDateFormat("yyyy-MM-dd"); //any format you want
                if (date!=null) {
                    String desiredDateFormat = newFormat_date.format(date);
                    holder.posted_on_date.setText(desiredDateFormat);
                }else{
                    holder.posted_on_date.setText("N/A");
                }

【讨论】:

  • 不,它总是返回为“Fri Nov 15 00:00:00 IST 2013”​​。感谢您的评论。
  • @SanatPandey 但如果您解析“2013 年 11 月 15 日”,那是正确的日期,不是吗?
【解决方案2】:

看看SimpleDateFormat的Android示例。

String[] formats = new String[] {
   "yyyy-MM-dd",
   "yyyy-MM-dd HH:mm",
   "yyyy-MM-dd HH:mmZ",
   "yyyy-MM-dd HH:mm:ss.SSSZ",
   "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
 };
 for (String format : formats) {
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.out.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.out.format("%30s %s\n", format, sdf.format(new Date(0)));
 }

输出:

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01
           yyyy-MM-dd HH:mm 1969-12-31 16:00
           yyyy-MM-dd HH:mm 1970-01-01 00:00
          yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
          yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
   yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
   yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000

【讨论】:

  • 在所有输出中,我没有得到与“MMMM DD, YYYY”相同的结果,例如:“2013 年 11 月 15 日”。请仅对此提出任何解决方案。感谢您的评论。
  • @SanatPandey Y 需要很小。 'MMMM DD,yyyy'
【解决方案3】:

我认为您不应该使用可变对象(例如 Date)作为 HashMap 的键。考虑将日期转换为字符串。这就是我格式化日期的方式。

DateFormat formatter1 = new SimpleDateFormat("MMMMM DD, yyyy");
System.out.println(formatter1.parse("15/02/2013"));

【讨论】:

  • 感谢您的评论,但我的问题有所不同。我希望格式字符串为“2013 年 11 月 15 日”,例如“mmmm dd, yyyy”。
  • 将 MMMM 更改为 MMMMM。
  • 在此之后结果也保持不变。
  • 2013 年 11 月 15 日星期五 00:00:00 IST
  • 但格式化程序总是返回 Date 对象。我们必须在格式化时取日期。
【解决方案4】:

由于 SimpleDateFormat 已弃用

我建议你使用这种方式

android.text.format.DateFormat dateFormat= new android.text.format.DateFormat();
dateFormat.format("MMMM DD, yyyy", new java.util.Date());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 2013-03-07
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多