【问题标题】:get day name from day number of the year android从一年中的日期获取日期名称android
【发布时间】:2017-04-17 04:31:11
【问题描述】:

我想从android中的一年中的天数获取星期几的名称。示例:2016 年第 344 天的日期名称是什么。我的服务器给了我这样的数据并且必须使用它来找出日期名称。

{
  "id": 106,
  "month": 12,
  "year": 2016,
  "yDay": 344,
  "hour": 19
}

我可以从日历中获取当前日期。

Calendar calendar = Calendar.getInstance(); int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);

【问题讨论】:

  • 你得到答案了吗?如果您没有得到,请尝试我的帖子。我已经实施并在最后检查过,对我来说工作正常。

标签: android datetime


【解决方案1】:

您可以从日期获取dayOfWeek(从yDay,month,year形成日期)

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

【讨论】:

  • 我只有一年中的天数,那么如何使用它。
  • @shihab_returns 鉴于您有月份编号,您可以减去截至月份的天数 - 1 以获得date
【解决方案2】:

首先,您需要使用年份和年份初始化一个Calendar 实例:

Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_YEAR, yourDayOfYear);
c.set(Calendar.YEAR, yourYear);

然后你可以从Calendar获取星期几

int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

【讨论】:

    【解决方案3】:

    通过以下方式实现它的一种方法:

     public String getNameOfDay(int year, int dayOfYear) {
    
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
        String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    
        int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
    
        return days[dayIndex - 1];
    }                    
    

    【讨论】:

    • 为什么是 days[dayIndex-1] ?
    • 我得到了解决方案。 dayIndex 给我 1-7 的价值。这就是为什么从我的 days 数组中减去一个值。
    【解决方案4】:

    您可以直接从 Calendar 对象中以字符串形式获取星期几:

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_YEAR, dayOfYear); // Set the day of the year value
    String day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US); 
    // Use Calendar.SHORT for abbreviated names or .LONG for full names
    // and be sure to change the locale as necessary
    

    【讨论】:

      【解决方案5】:

      What is the easiest way to get the current day of the week in Android?

      因此您将获得当前日期,并且您将以相同的方式获得当前日期以及相同日期的天数。现在计算一下

      【讨论】:

        【解决方案6】:

        试试这个,它会根据你的年份和月份给你名字

        // Assuming that you already have this.
            int year = 2016;
            int month = 12;
            int day = 2;
        
            // First convert to Date. This is one of the many ways.
            String dateString = String.format("%d-%d-%d", year, month, day);
            Date date = new SimpleDateFormat("yyyy-M-d").parse(dateString);
        
            // Then get the day of week from the Date based on specific locale.
            String dayOfWeek = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date);
        
            System.out.println(dayOfWeek); // Friday
        

        如果你有日期格式,那么你可以试试这个

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            dayName("2016-12-02 00:00:00", "YYYY-MM-DD HH:MM:ss")
        }
        
        public static String dayName(String inputDate, String format){
            Date date = null;
            try {
               date = new SimpleDateFormat(format).parse(inputDate);
            } catch (ParseException e) {
               e.printStackTrace();
            }
           return new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date);
        }
        

        【讨论】:

          【解决方案7】:

          实现此目的的另一种方法是使用 Joda Time Package。 Android 目前仅在 Java 1.7 上运行,因此本身不具备良好的时间特性。

          将 Joda Time 添加到您的build.gradle

          compile 'joda-time:joda-time:2.9.4'
          

          然后你可以做类似这样的事情:

          public String getNameOfDay(int year, int yDay) {
          
              String pattern = year + ";" + yDay;
          
              DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("y;D");
              DateTime requestedDate = DateTime.parse(pattern, dateTimeFormatter);
              int dayOfWeek = requestedDate.getDayOfWeek();
          
              //Notice that Monday is 1 and Sunday is 7
              String[] days = {"Monday", "Tuesday", "Thursday", "Friday", "Saturday", "Sunday"};
              return days[dayOfWeek - 1];
          }
          

          请注意,我只是提供了一种替代已发布的实现此目标的方法。如果您不想在您的项目中添加 joda 时间,您可以按照其他人已经说过的方式进行。

          【讨论】:

            【解决方案8】:

            这是我使用的方法,并确保您使用相同的格式,这意味着不要使用 - 而不是 /,反之亦然:

             public String getDayName(String date) {
                    SimpleDateFormat df = new SimpleDateFormat("M-d-yyyy", Locale.getDefault());
            
                    Date readDate=null;
                    try {
                        readDate = df.parse(date);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    Calendar c = Calendar.getInstance();
                    c.setTime(Objects.requireNonNull(readDate));
            
                    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
                    String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
            
                    return days[dayOfWeek-1];
                }
            

            【讨论】:

              猜你喜欢
              • 2015-02-24
              • 1970-01-01
              • 2016-11-14
              • 1970-01-01
              • 2015-10-21
              • 2021-08-21
              • 2020-08-22
              • 2012-01-12
              相关资源
              最近更新 更多