Vic*_*cky 59 java calendar date
我的输入字符串日期如下:
String date = "1/13/2012";
Run Code Online (Sandbox Code Playgroud)
我得到的月份如下:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = dateFormat.parse(date);
String month = new SimpleDateFormat("MM").format(convertedDate);
Run Code Online (Sandbox Code Playgroud)
但是如何在给定的字符串日期中获取该月的最后一个日历日?
例如:对于String "1/13/2012",输出必须是"1/31/2012".
Ale*_*r M 148
通过使用convertedDate.getMonth().length(convertedDate.isLeapYear())where convertedDate是一个实例LocalDate.
String date = "1/13/2012";
LocalDate convertedDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("M/d/yyyy"));
convertedDate = convertedDate.withDayOfMonth(
convertedDate.getMonth().length(convertedDate.isLeapYear()));
Run Code Online (Sandbox Code Playgroud)
通过使用getActualMaximum方法java.util.Calendar:
String date = "1/13/2012";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = dateFormat.parse(date);
Calendar c = Calendar.getInstance();
c.setTime(convertedDate);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
Run Code Online (Sandbox Code Playgroud)
SG *_* 86 25
这看起来像您的需求:
http://obscuredclarity.blogspot.de/2010/08/get-last-day-of-month-date-object-in.html
码:
import java.text.DateFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
//Java 1.4+ Compatible
//
// The following example code demonstrates how to get
// a Date object representing the last day of the month
// relative to a given Date object.
public class GetLastDayOfMonth {
public static void main(String[] args) {
Date today = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(today);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
Date lastDayOfMonth = calendar.getTime();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today : " + sdf.format(today));
System.out.println("Last Day of Month: " + sdf.format(lastDayOfMonth));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Today : 2010-08-03
Last Day of Month: 2010-08-31
Run Code Online (Sandbox Code Playgroud)
通过使用Java 8 java.time.LocalDate
String date = "1/13/2012";
LocalDate lastDayOfMonth = LocalDate.parse(date,DateTimeFormatter.ofPattern("M/dd/yyyy"));
.with(TemporalAdjusters.lastDayOfMonth());
Run Code Online (Sandbox Code Playgroud)
YearMonth // Represent the year and month, without a date and without a time zone.
.from( // Extract the year and month from a `LocalDate` (a year-month-day).
LocalDate // Represent a date without a time-of-day and without a time zone.
.parse( // Get a date from an input string.
"1/13/2012" , // Poor choice of format for a date. Educate the source of your data about the standard ISO 8601 formats to be used when exchanging date-time values as text.
DateTimeFormatter.ofPattern( "M/d/uuuu" ) // Specify a formatting pattern by which to parse the input string.
) // Returns a `LocalDate` object.
) // Returns a `YearMonth` object.
.atEndOfMonth() // Determines the last day of the month for that particular year-month, and returns a `LocalDate` object.
.toString() // Generate text representing the value of that `LocalDate` object using standard ISO 8601 format.
Run Code Online (Sandbox Code Playgroud)
2012-01-31
YearMonth这YearMonth门课让这一切变得容易。该atEndOfMonth方法返回一个LocalDate. 闰年二月是占。
首先定义一个格式化模式以匹配您的字符串输入。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "M/d/uuuu") ;
使用该格式化程序LocalDate从字符串输入中获取 a 。
String s = "1/13/2012" ;
LocalDate ld = LocalDate.parse( "1/13/2012" , f ) ;
Run Code Online (Sandbox Code Playgroud)
然后提取一个YearMonth对象。
YearMonth ym = YearMonth.from( ld ) ;
Run Code Online (Sandbox Code Playgroud)
要求YearMonth确定该年月份的最后一天,考虑2 月份的闰年。
LocalDate endOfMonth = ym.atEndOfMonth() ;
Run Code Online (Sandbox Code Playgroud)
以标准 ISO 8601 格式生成表示该日期的文本。
String output = endOfMonth.toString() ;
Run Code Online (Sandbox Code Playgroud)
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
从哪里获得 java.time 类?
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval,YearWeek,YearQuarter,和更多。
使用Java 8
DateTime/LocalDateTime:
String dateString = "01/13/2012";
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy", Locale.US);
LocalDate date = LocalDate.parse(dateString, dateFormat);
ValueRange range = date.range(ChronoField.DAY_OF_MONTH);
Long max = range.getMaximum();
LocalDate newDate = date.withDayOfMonth(max.intValue());
System.out.println(newDate);
Run Code Online (Sandbox Code Playgroud)
要么
String dateString = "01/13/2012";
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy", Locale.US);
LocalDate date = LocalDate.parse(dateString, dateFormat);
LocalDate newDate = date.withDayOfMonth(date.getMonth().length(date.isLeapYear()));
System.out.println(newDate);
Run Code Online (Sandbox Code Playgroud)
输出:
2012-01-31
Run Code Online (Sandbox Code Playgroud)
LocalDateTime应该使用,而不是LocalDate在日期字符串中有时间信息.IE2015/07/22 16:49
小智 5
Java 8 及更高版本:
import java.time.LocalDate;
import java.time.Year;
static int lastDayOfMonth(int Y, int M) {
return LocalDate.of(Y, M, 1).getMonth().length(Year.of(Y).isLeap());
}
Run Code Online (Sandbox Code Playgroud)
以 Basil Bourque 的评论为准
import java.time.YearMonth;
int lastDayOfMonth = YearMonth.of(Y, M).lengthOfMonth();
Run Code Online (Sandbox Code Playgroud)