按年,月,日查找日期名称

如果我在Java中有int year,int month,int day,那么如何查找日期名称? 这已经有了一些function吗?

使用带有EEEE模式的SimpleDateFormat来获取星期几的名称。

 // Assuming that you already have this. int year = 2011; int month = 7; int day = 22; // 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-Md").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 

在这里,它被包装成一个很好的Java类。

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class DateUtility { public static void main(String args[]){ System.out.println(dayName("2015-03-05 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); } } 

您可以执行以下操作来获取不同区域设置的星期几的名称。

这是重要的部分:

 DateFormatSymbols dfs = new DateFormatSymbols(usersLocale); String weekdays[] = dfs.getWeekdays(); 

这可以与此结合:

 Calendar cal = Calendar.getInstance(); int day = cal.get(Calendar.DAY_OF_WEEK); 

要获得您想要的东西:

 String nameOfDay = weekdays[day]; 

工作日的名称因地区而异。 因此,您必须使用具有适当区域设置的DateFormat 。 例如:

 SimpleDateFormat format = new SimpleDateFormat("EEEE"); System.out.println(format.format(date)); 

Date对象可以通过多种方式获得,包括不推荐使用的Date(..)构造函数, Calendar.set(..)方法或joda-time DateTime 。 (对于后者,你可以使用joda-time自己的DateTimeFormat

使用年,月和日构建GregorianCalendar,然后查询它以查找当天的名称。 像这样的东西:

 int year = 1977; int month = 2; int dayOfMonth = 15; Calendar myCalendar = new GregorianCalendar(year, month, dayOfMonth); int dayOfWeek = myCalendar.get(Calendar.DAY_OF_WEEK); 

请注意,星期几作为int表示,表示区域设置的工作日表示中的日期序号 。 IE,在工作日星期一开始并在星期日结束的地方,2表示星期二,而如果工作日工作日星期日开始,则相同的2表示星期一。

编辑

由于有很多答案编辑正在进行,请允许我添加以下内容:

 DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault()); String dayOfMonthStr = symbols.getWeekdays()[dayOfMonth]; 

老实说,我更喜欢SimpleDateFormatter方法,因为它封装了与上面所示完全相同的代码。 愚蠢的我忘掉了一切。

您可以使用日历对象来查找它。

创建日历实例后,您将获得DAY_OF_WEEK(这是一个int),然后您可以从那里找到那一天)

您可以像这样使用switch语句:

 import java.util.*; public class DayOfWeek { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); int day = cal.get(Calendar.DAY_OF_WEEK); System.out.print("Today is "); switch (day) { case 1: System.out.print("Sunday"); break; case 2: System.out.print("Monday"); break; case 3: System.out.print("Tuesday"); break; case 4: System.out.print("Wednesday"); break; case 5: System.out.print("Thursday"); break; case 6: System.out.print("Friday"); break; case 7: System.out.print("Saturday"); } System.out.print("."); } } 
 Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 22); //Set Day of the Month, 1..31 cal.set(Calendar.MONTH,6); //Set month, starts with JANUARY = 0 cal.set(Calendar.YEAR,2011); //Set year System.out.println(cal.get(Calendar.DAY_OF_WEEK)); //Starts with Sunday, 6 = friday 
 new GregorianCalendar().setTime(new Date()).get(DAY_OF_WEEK) 

这给你一个数字, Calendar.SUNDAY == 1Calendar.MONDAY == 2 ,…

是的,但这对JDK来说是一个相当漫长的过程。 JodaTime可能是更好的选择(我没有使用它)。

首先,您获得一个Calendar对象,以便您可以从日/月/年/时区构建日期。 不要使用其中一个已弃用的Date构造函数。

然后从该日历中获取Date对象,并将其传递给SimpleDateFormat 。 请注意,格式对象不是线程安全的。

  // by default, this Calendar object will have the current timezone Calendar cal = GregorianCalendar.getInstance(); cal.set(2011, 6, 22); // this formatter will have the current locale SimpleDateFormat format = new SimpleDateFormat("EEEE"); System.out.println(format.format(cal.getTime())); 

使用Joda-Time库时,这种日期时间工作更容易。 一个简单的单线程。

 String dayOfWeek = new LocalDate( 2014, 1, 2 ).dayOfWeek().getAsText( java.util.Locale.ENGLISH ); System.out.println( "dayOfWeek: " + dayOfWeek ); 

跑的时候……

 dayOfWeek: Thursday