获得给定stringdate中的最后一天
我的inputstringdate如下所示:
String date = "1/13/2012";
我得到的月份如下:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); Date convertedDate = dateFormat.parse(date); String month = new SimpleDateFormat("MM").format(convertedDate);
但是,如何获得给定stringdate中的最后一个日历日?
例如对于string“2012年1月13日”输出应该作为date“2012年1月31日”
Java 8及以上。
通过使用convertedDate.getMonth().length(convertedDate.isLeapYear())
,其中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()));
Java 7及以下版本。
通过使用java.util.Calendar
getActualMaximum
方法:
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));
这看起来像你的需求:
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)); } }
输出:
Today : 2010-08-03 Last Day of Month: 2010-08-31
使用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);
要么
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().maxLength()); System.out.println(newDate);
输出:
2012-01-31
如果在datestring中有时间信息,则应使用
LocalDate
而不是LocalDate
。 IE2015/07/22 16:49
通过使用java 8 java.time.LocalDate
String date = "1/13/2012"; LocalDate localDate = LocalDate.parse(date,DateTimeFormatter.ofPattern("MM/dd/yyyy")); lastDayOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
最简单的方法是构造一个新的GregorianCalendar
实例,如下所示:
Calendar cal = new GregorianCalendar(2013, 5, 0); Date date = cal.getTime(); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println("Date : " + sdf.format(date));
输出:
Date : 2013-05-31
注意:
月份用于设置日历中MONTH日历字段的值。 月份值为0,例如1月份为0。
使用GregorianCalendar
。 设置对象的date,然后使用getActualMaximum(Calendar.DAY_IN_MONTH)
。
http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#getActualMaximum%28int%29 (但它在Java 1.4中是一样的)
您可以使用下面的代码来获取该月的最后一天
public static String getLastDayOfTheMonth(String date) { String lastDayOfTheMonth = ""; SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); try{ java.util.Date dt= formatter.parse(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(dt); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); java.util.Date lastDay = calendar.getTime(); lastDayOfTheMonth = formatter.format(lastDay); } catch (ParseException e) { e.printStackTrace(); } return lastDayOfTheMonth; }
这对我来说工作得很好
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone()); cal.set(Calendar.MONTH, month-1); cal.set(Calendar.YEAR, year); cal.add(Calendar.DATE, -1); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis();
public static String getLastDayOfMonth(int year, int month) throws Exception{ DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(year+"-"+(month<10?("0"+month):month)+"-01"); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); Date lastDayOfMonth = calendar.getTime(); return sdf.format(lastDayOfMonth); } public static void main(String[] args) throws Exception{ System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 1)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 3)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 4)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 5)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 6)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 7)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 8)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 9)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 10)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 11)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 12)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2018, 1)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2018, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2018, 3)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2010, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2011, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2012, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2013, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2014, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2015, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2016, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2017, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2018, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2019, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2020, 2)); System.out.println("Last Day of Month: " + getLastDayOfMonth(2021, 2)); }
输出:
每月的最后一天:2017-01-31
2017年的最后一天:2017-02-28
每月的最后一天:2017-03-31
每月的最后一天:2017-04-30
2017年的最后一天:2017-05-31
每月的最后一天:2017-06-30
本月的最后一天:2017-07-31
本月最后一天:2017-08-31
每月的最后一天:2017-09-30
本月最后一天:2017-10-31
本月最后一天:2017-11-30
每月的最后一天:2017-12-31
本月最后一天:2018-01-31
本月最后一天:2018-02-28
本月最后一天:2018-03-31
本月最后一天:2010-02-28
本月最后一天:2011-02-28
每月的最后一天:2012-02-29
本月最后一天:2013-02-28
每月的最后一天:2014-02-28
本月最后一天:2015-02-28
每月的最后一天:2016-02-29
2017年的最后一天:2017-02-28
本月最后一天:2018-02-28
本月最后一天:2019-02-28
本月最后一天:2020-02-29
每月的最后一天:2021-02-28
我在JasperServer报告中使用这一行:
new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("yyyy-MM-dd").parse(new java.util.Date().format('yyyy') + "-" + (new Integer (new SimpleDateFormat("MM").format(new Date()))+1) + "-01")-1)
不好看,但为我工作。 基本上它是加1当前月份,获得该月的第一天,减去一天。