2011-08-29 79 views
36

我正在尝试检索当月的哪一天。如何获取当月的某天?

如今天是2011年8月29日。

我想要做的就是获得天数,如29或30。这是哪一天。

我该如何去做这件事?

+0

快速搜索想出了这个.. http://www.roseindia.net/java/beginners/DayYearToDayMonth.shtml –

回答

68

你想做得到一个日历实例,并得到它的月

Calendar cal = Calendar.getInstance(); 
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); 

String dayOfMonthStr = String.valueOf(dayOfMonth); 

您还可以得到DAY_OF_WEEK,DAY_OF_YEAR,DAY_OF_WEEK_IN_MONTH等

+1

反正有没有得到这个作为一个字符串? – yoshi24

+0

代码更新后续问题 – hooked82

2

看看GregorianCalendar,有事的一天如:

final Calendar now = GregorianCalendar.getInstance() 
final int dayNumber = now.get(Calendar.DAY_OF_MONTH); 
1

您可以从阅读Date的文档开始。然后你意识到Date的方法全部被弃用,转而使用Calender

Calendar now = Calendar.getInstance(); 
System.out.println(now.get(Calendar.DAY_OF_MONTH)); 
11

下面的方法会帮助你找到任何指定日期的当天:

public static int getDayOfMonth(Date aDate) { 
    Calendar cal = Calendar.getInstance(); 
    cal.setTime(aDate); 
    return cal.get(Calendar.DAY_OF_MONTH); 
} 
3

TL;博士

LocalDate.now().getDayOfMonth()  // Better to pass a `ZoneId` optional argument to `now` as shown below than rely implicitly on the JVM’s current default time zone. 

java.time

现代化的方法是LocalDate类来表示仅限日期的值。

时区对确定当前日期至关重要。对于任何特定的时刻,日期因地区而异。

ZoneId z = ZoneId.of("America/Montreal"); 
LocalDate ld = LocalDate.now(z) ; 
int dayOfMonth = ld.getDayOfMonth(); 

您还可以获得星期几。

DayOfWeek dow = ld.getDayOfWeek(); 

关于java.time

java.time框架是建立在Java 8和更高版本。这些类代替了日期时间类legacy,如java.util.Date,Calendar,& SimpleDateFormat

Joda-Time项目,现在在maintenance mode,建议迁移到java.time类。请参阅Oracle Tutorial。并搜索堆栈溢出了很多例子和解释。规格是JSR 310

使用JDBC driverJDBC 4.2或更高版本兼容的,你可以交换java.time直接对象与数据库。不需要字符串和java.sql。*类。

从何处获取java.time类?

ThreeTen-Extra项目与其他类扩展java.time。这个项目是未来可能增加java.time的一个试验场。您可以在这里找到一些有用的类,如Interval,YearWeek,YearQuartermore


乔达时间

UPDATE:的乔达时间项目现在处于维护模式,并建议迁移到java.time类。本节保留完整的历史记录。

使用Joda-Time 2.5库而非众所周知的烦人的java.util.Date和.Calendar类。

时区对确定日期至关重要。最好指定区域,而不是隐式地依赖JVM当前分配的默认时区。

DateTimeZone zone = DateTimeZone.forID("America/Montreal"); 
DateTime now = DateTime.now(zone).withTimeAtStartOfDay(); 
int dayOfMonth = now.getDayOfMonth(); 

或者使用类似的代码与具有时间的天没有部分LocalDate类。

-2
//This will get you the day of the month 
String.valueOf((Calendar.getInstance()).get(Calendar.DAY_OF_MONTH)) 
+0

请编辑更多信息。仅限代码和“尝试这个”的答案是不鼓励的,因为它们不包含可搜索的内容,也不解释为什么有人应该“尝试这个”。 – abarisone

+0

请解释一下,以便人们可以更好地了解您的代码正在做什么。 – cammando

0

的Java 8更新

的Java 8引入了时间和日期操作以下软件包。

java.time.*; 
java.time.format.*; 

java.time.chono.*;  
java.time.temporal.*; 
java.time.zone.*; 

这些更有组织和直观。

我们只需讨论最多两个包。
有3个顶级类别 - LocalDate,LocalTime,LocalDateTime分别用于描述日期,时间和日期时间。虽然它们在toString()中格式正确,但每个类都有格式化方法,它接受DateTimeFormatter以自定义的方式进行格式化。

DateTimeFormatter也可以用来显示每天给定的日期。它有几个

import java.time.*; 
import java.time.format.*; 

class DateTimeDemo{ 
    public static void main(String...args){ 
     LocalDateTime x = LocalDateTime.now(); 
     System.out.println(x.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)));//Shows Day and Date. 

     System.out.println(x.format(DateTimeFormatter.ofPattern("EE")));//Short Form 
     System.out.println(x.format(DateTimeFormatter.ofPattern("EEEE")));//Long Form 

    } 
} 

ofLocalizedTime接受FormatStyle这在java.time.format

ofPattern枚举接受String受限长度的限制模式字符。以下是可以传入toPattern方法的字符。

enter image description here

你可以尝试不同数量的模式来查看输出将会如何。

签出更多关于Java 8日期时间API here