如何获得一个月第一天的名字?

如何获得本月的第一天? 因此,对于今年1月份,它将在周日返回。 然后在二月它将在周三返回。

要获取当前月份的第一个日期,请使用java.util.Calendar 。 首先获取它的一个实例,并将字段Calendar.DAY_OF_MONTH设置为该月的第一个日期。 由于任何月份的第一天是1,而cal.getActualMinimum(Calendar.DAY_OF_MONTH) ,这里可以使用1。

 private Date getFirstDateOfCurrentMonth() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); return cal.getTime(); } 

您可以使用任何日期创建Calendar ,然后set(Calendar.DAY_OF_MONTH, 1)以获取一个月的第一天。

  Calendar cal = Calendar.getInstance(); cal.set(Calendar.DATE, 25); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.YEAR, 2012); cal.set(Calendar.DAY_OF_MONTH, 1); Date firstDayOfMonth = cal.getTime(); DateFormat sdf = new SimpleDateFormat("EEEEEEEE"); System.out.println("First Day of Month: " + sdf.format(firstDayOfMonth)); 
 public int getFirstDay(){ Calendar c=new GregorianCalendar(); c.set(Calendar.DAY_OF_MONTH, 1); return c.get(Calendar.DAY_OF_WEEK); } 

从那里你可以看到int是否等于Calendar.SUNDAY,Calendar.MONDAY等。

创建java.util.Datejava.util.Calendar对象,设置日期值并使用java.text.SimpleDateFormat类方法对其进行格式化。

  Calendar cal=Calendar.getInstance(); cal.set(Calendar.DATE,1); cal.set(Calendar.MONTH,0); cal.set(Calendar.YEAR,2012); SimpleDateFormat sdf=new SimpleDateFormat("EEEE"); System.out.println(sdf.format(cal.getTime())); 

在Java 8中,您可以使用TemporalAdjusters:

这是一个例子:

 import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; /** * Dates in Java8 * */ public class App { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); System.out.println("Day of Month: " + localDate.getDayOfMonth()); System.out.println("Month: " + localDate.getMonth()); System.out.println("Year: " + localDate.getYear()); System.out.printf("first day of Month: %s%n", localDate.with(TemporalAdjusters.firstDayOfMonth())); System.out.printf("first Monday of Month: %s%n", localDate .with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY))); System.out.printf("last day of Month: %s%n", localDate.with(TemporalAdjusters.lastDayOfMonth())); System.out.printf("first day of next Month: %s%n", localDate.with(TemporalAdjusters.firstDayOfNextMonth())); System.out.printf("first day of next Year: %s%n", localDate.with(TemporalAdjusters.firstDayOfNextYear())); System.out.printf("first day of Year: %s%n", localDate.with(TemporalAdjusters.firstDayOfYear())); LocalDate tomorrow = localDate.plusDays(1); System.out.println("Day of Month: " + tomorrow.getDayOfMonth()); System.out.println("Month: " + tomorrow.getMonth()); System.out.println("Year: " + tomorrow.getYear()); } } 

结果将是:

 Day of Month: 16 Month: MAY Year: 2014 first day of Month: 2014-05-01 first Monday of Month: 2014-05-05 last day of Month: 2014-05-31 first day of next Month: 2014-06-01 first day of next Year: 2015-01-01 first day of Year: 2014-01-01 Last in Month Tuesday: 2014-05-27 Day of Month: 17 Month: MAY Year: 2014 

java.time

从Java 8开始,我们也可以使用YearMonth类,它允许我们使用指定的天数(first,last)创建LocalDate对象。 然后我们可以简单地将这些日期转换为DayOfWeek Enum ( 教程 )并阅读其name属性。

 YearMonth ym = YearMonth.of(2012, 1); String firstDay = ym.atDay(1).getDayOfWeek().name(); String lastDay = ym.atEndOfMonth().getDayOfWeek().name(); System.out.println(firstDay); System.out.println(lastDay); 

结果:

 SUNDAY TUESDAY 

java.time,软编码

Pshemo的答案很好而且很聪明,但很难用英语编写。 让我们来看看它是否允许本地化。

 ZoneId zoneId = ZoneId.of( "America/Montreal" ); // Time zone is crucial to determining a date. YearMonth yearMonth = YearMonth.now( zoneId ); LocalDate firstOfMonth = yearMonth.atDay( 1 ); 

格式化

生成该LocalDate的文本表示。 我们指定了一个所需的Locale ,在本例中为CANADA_FRENCH 。 如果省略,则隐式应用JVM的当前默认Locale; 最好指定显式的Locale。

 DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "EEEE" ).withZone( zoneId ).withLocale( Locale.CANADA_FRENCH ); // Exactly four 'E' letters means full form. http://docs.oracle.com/javase/8/docs/api/java/time/format/TextStyle.html#FULL String output = formatter.format( firstOfMonth ); 

转储到控制台。

 System.out.println( "output: " + output ); 

跑步的时候。

samedi

DayOfWeek Enum

另一种方法是使用DayOfWeek枚举。 此枚举包含一个getDisplayName方法,您可以在其中指定文本样式 (全名与缩写)和Locale

 DayOfWeek dayOfWeek = firstOfMonth.getDayOfWeek() ; String output = dayOfWeek.getDisplayName( TextStyle.FULL_STANDALONE , Locale.CANADA_FRENCH ) ;