【问题标题】:DateFormat conversion in AndroidAndroid中的日期格式转换
【发布时间】:2015-08-03 09:52:33
【问题描述】:

假设我们有一个日期显示为。

2015-08-03 12:00:00

如何将其转换为一天的名称,例如 Tuesday ?我不想要03 Tue 之类的东西。只需要完整的days 名称。我环顾四周,但对此我有点困惑。

【问题讨论】:

标签: java android date days


【解决方案1】:

首先,将该日期解析为 java.util.Date 对象。

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date yourDate = formatter.parse("2015-08-03 12:00:00");

然后,使用此日期填充 Calendar

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

现在你有你的星期几dayOfWeek(例如,1 是星期天)。

【讨论】:

    【解决方案2】:
     SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     Date now = simpleDateformat.parse("2015-08-03 12:00:00");
     simpleDateformat = new SimpleDateFormat("EEEE"); // the day of the week spelled out completely
     System.out.println(simpleDateformat.format(now));
    

    【讨论】:

    • 请停止编辑帖子以在任何地方添加代码格式,这比其他任何事情都更让人分心,我已经不得不修改您的 2 个编辑以使帖子再次可读。谢谢。
    • 好吧,在我编辑之前,这些问题更难看。对不起,如果我这样做了..
    • 例如,在上面的问题中,您在datedays这两个词中添加了代码格式,但这不是代码,所以它添加不正确将注意力吸引到问题的不重要部分的格式。
    【解决方案3】:

    这是我想出的解决方案:

         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  
           HH:mm:ss");
          Date weekDay = null;
          try {
              weekDay = formatter.parse("2015-08-03 12:00:00");
          } catch (ParseException e) {
              e.printStackTrace();
           }
         SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
    
         String day = outFormat.format(weekDay);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 2020-11-24
      • 2017-12-23
      相关资源
      最近更新 更多