使用Calendar类在Java中获取星期几作为特定日期的字符串

Slashdottir

我想用Java做这个简单的事情。正确答案应该是“星期三”。这有什么问题?我从Calendar类的文档中获取了示例。

import java.util.*;
import java.lang.Integer;

class Dumb {
  public static List<String> dows = Collections.unmodifiableList(Arrays.asList("SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"));

  public static String getDay(String day, String month, String year) {
   // get the supported ids for GMT-08:00 (Pacific Standard Time)
   String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
   // if no ids were returned, something is wrong. get out.
   if (ids.length == 0)
       System.exit(0);
   // create a Pacific Standard Time time zone
   SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
   // set up rules for Daylight Saving Time
   pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
   pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

   // create a GregorianCalendar with the Pacific Daylight time zone
   // and the desired date and time
   Calendar calendar = new GregorianCalendar(pdt);
   calendar.set(Calendar.MONTH, Integer.valueOf(month));
   calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(day));
   calendar.set(Calendar.YEAR, Integer.valueOf(year)); 

   // print out a bunch of interesting things
   System.out.println("SUNDAY: " + calendar.SUNDAY);
   System.out.println("MONDAY: " + calendar.MONDAY);
   System.out.println("TUESDAY: " + calendar.TUESDAY);
   System.out.println("WEDNESDAY: " + calendar.WEDNESDAY);
   System.out.println("THURSDAY: " + calendar.THURSDAY);
   System.out.println("FRIDAY: " + calendar.FRIDAY);
   System.out.println("SATURDAY: " + calendar.SATURDAY);
   System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
   System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
   System.out.println("DATE: " + calendar.get(Calendar.DATE));
   System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
  int dow = calendar.get(Calendar.DAY_OF_WEEK);
  return dows.get(dow-1);
    }
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String month = in.next();
    String day = in.next();
    String year = in.next();

    System.out.println(getDay(day, month, year));
}

}

此代码产生以下输出:

SUNDAY: 1 
MONDAY: 2
TUESDAY: 3
WEDNESDAY: 4
THURSDAY: 5
FRIDAY: 6
SATURDAY: 7
YEAR: 2015
MONTH: 8
DATE: 5
DAY_OF_WEEK: 7
SATURDAY
Slashdottir

因为月份是从零开始的。

我应该从月份中减去1。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何获取特定日期是星期几?

字符串中的Java星期几

从星期几整数Java获取星期几字符串

Objective-C / iOS:获取星期几(星期日)中特定日期的日期

过滤特定星期几的字符串日期列表

在Java中,如何使用系统的默认语言环境(语言)获取星期几的字符串(星期日,星期一,...,星期六)

javascript:根据特定日期获取星期几

使用Powershell脚本获取字符串中具有特定日期的文件

php在特定日期后的第一个星期一作为字符串

获取指定日期的星期几

使用Java日历获取一年中的星期几,将星期日作为星期几

给定 ISO 日期,以 ISO 字符串范围的形式获取星期几

将日期字符串转换为星期几

PHP,以字符串形式标识日期和星期几

如何使用“星期几”和“星期几”的格式字符串

在JodaTime中是否可以将星期几作为仅具有星期几的整数的字符串而无需创建DateTime对象

使用Java Calendar API的特定日期

在Java中获取日期的星期几的字母缩写

如何通过传递特定日期来确定星期几?

获取两个选定日期之间的星期几

Spark中的星期几日期格式字符串Java

使用Moment.js查找当前星期几的特定日期

如何从表中的特定日期显示月份(作为字符串)和年份(4 位数字)?

获取星期几的日期

从日期获取星期几

如何创建一个将字符串作为输入并在R中返回星期几的向量的函数?

如何使用java.time从字符串中解析年份和星期的日期

如何从Swift中的日期获取星期几

从星期几中获取日期 Javascript