简体   繁体   English

从用户输入中获取星期几

[英]Getting the day of the week from user input

Date date= (new GregorianCalendar(year, month, day)).getTime();
SimpleDateFormat f = new SimpleDateFormat("EEEE");
String dow=f.format(date);

System.out.print("This date is a "+dow);

I have the user input a month(1-12) a day(1-31) and a year(1600-2400) It works fine only it displays the wrong day. 我让用户输入一个月(1-12),一天(1-31)和一年(1600-2400),但它只在显示错误的日期时有效。 For example it says that Jan 1st 2014 is a Saturday but it was a Wednesday. 例如,它说2014年1月1日是星期六,但是那是星期三。 It is probably because I didn't factor in leap years but I don't have a clue to go about doing that. 可能是因为我没有考虑leap年,但我没有线索去做。 Neither do I know how to tell it how many days in each month. 我也不知道如何告诉每个月多少天。 An array? 数组? Hopefully minimal lines as well. 希望最少的行也是如此。

Thanks so much!!! 非常感谢!!! This has been bugging me for an hour +. 这已经困扰了我一个多小时。 And something so simple, I should have figured. 和一些简单的事情,我应该已经想到了。 I must be tired. 我一定累了

Thanks!!!!!!! 谢谢!!!!!!!

Month is Zero based. Month基于零。 Try, 尝试,

Date date= (new GregorianCalendar(year, month-1, day)).getTime();
    SimpleDateFormat f = new SimpleDateFormat("EEEE");
    String dow=f.format(date);

The answer by Shashank Kadne is correct. Shashank Kadne答案是正确的。

Joda-Time 乔达时代

FYI, this work is simpler and cleaner using the Joda-Time 2.3 library. 仅供参考,使用Joda-Time 2.3库,这项工作更简单,更简洁。

Joda-Time uses sensible one-based counting for things such as: Joda-Time对诸如以下内容使用明智的基于一点的计数:

  • Month-of-Year 年度月份
    January = 1, February = 2, and so on. 一月= 1,二月= 2,依此类推。
  • Day-of-Week 星期几
    Monday = 1, Sunday = 7. (Standard ISO 8601 week) 星期一= 1,星期日=7。(标准ISO 8601周)

Joda-Time DateTime objects know their own time zone, unlike java.util.Date objects. java.util.Date对象不同,Joda-Time DateTime对象知道自己的时区。

Joda-Time leverages a specified Locale object to render localized strings. Joda-Time利用指定的Locale对象呈现本地化的字符串。

Example Code 范例程式码

// Specify a time zone rather than rely on default.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );

int year = 2014;
int month = 1; // Sensible one-based counting. January = 1, February = 2, …
int dayOfMonth = 2;

DateTime dateTime = new DateTime( year, month, dayOfMonth, 0, 0, 0, timeZone );

// Day-of-week info.
int dayOfWeekNumber = dateTime.getDayOfWeek(); // Standard week (ISO 8601). Monday = 1, Sunday = 7.
DateTime.Property dayOfWeekProperty = dateTime.dayOfWeek();
String dayOfWeekName_Short = dayOfWeekProperty.getAsShortText( Locale.FRANCE );
String dayOfWeekName_Long = dayOfWeekProperty.getAsText( Locale.FRANCE );

Dump to console… 转储到控制台...

System.out.println( "dateTime: " + dateTime );
System.out.println( "dayOfWeekNumber: " + dayOfWeekNumber );
System.out.println( "dayOfWeekName_Short: " + dayOfWeekName_Short );
System.out.println( "dayOfWeekName_Long: " + dayOfWeekName_Long );

When run… 运行时...

dateTime: 2014-01-02T00:00:00.000+01:00
dayOfWeekNumber: 4
dayOfWeekName_Short: jeu.
dayOfWeekName_Long: jeudi

Without Time & Time Zone 没有时间和时区

If you truly want only date without any time or time zone, then write similar code but with the LocalDate class. 如果您确实只希望没有任何时间或时区的日期,请编写类似的代码,但要使用LocalDate类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM