Java 如何获得每月的第二天?
我正在努力找回这个月的哪一天 比如今天是2011年8月29日 我想做的就是得到天数,比如29天或30天。一个月的哪一天Java 如何获得每月的第二天?,java,android,Java,Android,我正在努力找回这个月的哪一天 比如今天是2011年8月29日 我想做的就是得到天数,比如29天或30天。一个月的哪一天 我该怎么做呢?您需要获取一个日历实例并获取它的月日 Calendar cal = Calendar.getInstance(); int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); String dayOfMonthStr = String.valueOf(dayOfMonth); 您还可以获取周中的日、年中的日、月中的周中的日
我该怎么做呢?您需要获取一个日历实例并获取它的月日
Calendar cal = Calendar.getInstance();
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
String dayOfMonthStr = String.valueOf(dayOfMonth);
您还可以获取周中的日、年中的日、月中的周中的日等。请查看以下内容:
final Calendar now = GregorianCalendar.getInstance()
final int dayNumber = now.get(Calendar.DAY_OF_MONTH);
您可以从阅读文档开始。然后您意识到Date的方法都已被弃用,转而使用
以下方法将帮助您查找任何指定日期的日期:
public static int getDayOfMonth(Date aDate) {
Calendar cal = Calendar.getInstance();
cal.setTime(aDate);
return cal.get(Calendar.DAY_OF_MONTH);
}
tl;博士
java.time
现代方法是使用LocalDate
类来表示仅限日期的值
时区对于确定当前日期至关重要。在任何一个特定的时刻,世界各地的日期都因地区而异
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate ld = LocalDate.now( z ) ;
int dayOfMonth = ld.getDayOfMonth();
你也可以得到一周中的哪一天
DayOfWeek dow = ld.getDayOfWeek();
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,& 该项目现已启动,建议迁移到类 要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是 使用兼容的或更高版本,您可以直接与数据库交换java.time对象。不需要字符串,也不需要java.sql.*类 从哪里获得java.time类
- ,及以后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 更高版本的Android捆绑包实现了java.time类
- 对于早期的Android,该项目采用了ThreeTen Backport(如上所述)。看
乔达时间 更新:Joda Time项目现在处于维护模式,并建议迁移到java.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
类使用类似的代码。Java8更新
//This will get you the day of the month
String.valueOf((Calendar.getInstance()).get(Calendar.DAY_OF_MONTH))
Java8为时间和日期操作引入了以下包
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
}
}
LocalizedTime的接受FormatStyle
,这是java.time.format
模式的接受长度受限的模式字符的字符串。以下是可以传递到toPattern
方法中的字符
您可以尝试不同数量的模式来查看输出结果
查看更多关于Java8日期时API的信息,Java8版本简化了很多。我在下面给出了一些util方法
以int
格式获取给定日期、月份和年份的月份日期
public static int findDay(最终整数月、最终整数日、最终整数年){
//System.out.println(LocalDate.of(年、月、日).getDayOfMonth());
返回LocalDate.of(年、月、日).getDayOfMonth();
}
以int
格式获取当月的当前日期
public static int findDay(最终整数月、最终整数日、最终整数年){
//System.out.println(LocalDate.now(ZoneId.of(“亚洲/加尔各答”)).getDayOfMonth());
返回LocalDate.now(ZoneId.of(“亚洲/加尔各答”)).getDayOfMonth();
}
以字符串格式获取给定日期、月份和年份的星期几
公共静态字符串findDay(最终整数月、最终整数日、最终整数年){
//System.out.println(LocalDate.of(年、月、日).getDayOfWeek());
返回LocalDate.of(年、月、日).getDayOfWeek().toString();
}
以字符串格式获取一周中的当前日期
公共静态字符串findDay(最终整数月、最终整数日、最终整数年){
//System.out.println(LocalDate.now(ZoneId.of(“Asia/Kolkata”))…getDayOfWeek();
返回LocalDate.now(ZoneId.of(“亚洲/加尔各答”)).getDayOfWeek().toString();
}
LocalDate=新日期();
date.lengthOfMonth()
或
DateTimeFormatter formatter=模式的DateTimeFormatter.of(“YYYY-MM-dd”)
DateTimeFormatter monthFormatter=模式的DateTimeFormatter.of(“MMM”)
String stringDate=formatter.format(日期)
字符串month=monthFormatter.format(日期) 一次快速搜索发现了这个。。还有其他方法可以将其作为字符串吗?仅供参考,诸如java.util.date
,java.util.Calendar
,&java.text.SimpleDateFormat
等麻烦的日期时间类现在是遗留的,被这些类所取代。大多数java.time功能都在中向后移植到Java6和Java7
java.time.*;
java.time.format.*;
java.time.chono.*;
java.time.temporal.*;
java.time.zone.*;
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
}
}