Java 如何获取上个月的第一个日期和最后一个日期?(爪哇)

Java 如何获取上个月的第一个日期和最后一个日期?(爪哇),java,datetime,date,Java,Datetime,Date,根据今天的日期,我应该得到上个月的第一个日期和最后一个日期。我想不出任何逻辑 例如,通过05/30/2012,我应该获得04/01/2012和04/30/2012 任何帮助都将不胜感激。谢谢。使用 输出: 04/01/12 04/30/12 2012-04-01T00:00:00.000+02:00 2012-04-30T00:00:00.000+02:00 说明:DateMidnight对象是一个没有时间信息的日期对象,这似乎正是您所需要的。如果没有,请将上述代码中出现的所有DateMidn

根据今天的日期,我应该得到上个月的第一个日期和最后一个日期。我想不出任何逻辑

例如,通过
05/30/2012
,我应该获得
04/01/2012
04/30/2012

任何帮助都将不胜感激。谢谢。

使用

输出:

04/01/12
04/30/12
2012-04-01T00:00:00.000+02:00
2012-04-30T00:00:00.000+02:00


说明:DateMidnight对象是一个没有时间信息的日期对象,这似乎正是您所需要的。如果没有,请将上述代码中出现的所有DateMidnight替换为DateTime。

您可以创建一个日历对象,将日期设置为当前月份的第一天(应该是第一天:p),然后可以执行两个操作: 您可以从日历对象中减去特定的时间段,例如一个月(这将为您提供上个月的第一个日期,或一天,这将为您提供上个月的最后一天。我没有尝试过,但这将是我的第一步。


    • 还想在Jigar的答案中添加一些内容。使用
      DateFormat
      类以您在问题中指定的格式获取日期:

      DateFormat df = DateFormat.getInstance(DateFormat.SHORT);
      System.out.println(df.format(firstDateOfPreviousMonth));
      System.out.println(df.format(lastDateOfPreviousMonth));
      
      输出:

      04/01/12
      04/30/12
      
      太长,读不下去了 ……还有

      YearMonth.now().minusMonths( 1 ).atEndOfMonth()
      
      避免j.u.Date 避免使用捆绑的
      java.util.Date
      .Calendar
      类,因为它们非常麻烦。相反,请使用java 8及更高版本中的

      java.time Java8()中的新版本提供了相应的命令

      恰当命名的类表示一年中的一个月,没有任何特定的日期或时间。从那里我们可以询问该月的第一天和最后几天

      ZoneId zoneId = ZoneId.of( "America/Montreal" );
      YearMonth yearMonthNow = YearMonth.now( zoneId );
      YearMonth yearMonthPrevious = yearMonthNow.minusMonths( 1 );
      LocalDate firstOfMonth = yearMonthPrevious.atDay( 1 );
      LocalDate lastOfMonth = yearMonthPrevious.atEndOfMonth();
      
      乔达时间 更新:项目现在开始,团队建议迁移到类。请参阅

      在2.8中,如果您不关心一天中的时间,请使用
      LocalDate

      LocalDate today = LocalDate.now( DateTimeZone.forID( "Europe/Paris" ) );
      LocalDate firstOfThisMonth = today.withDayOfMonth( 1 );
      LocalDate firstOfLastMonth = firstOfThisMonth.minusMonths( 1 );
      LocalDate endOfLastMonth = firstOfThisMonth.minusDays( 1 );
      

      关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,如,&

      该项目现已启动,建议迁移到类

      要了解更多信息,请参阅.和搜索堆栈溢出以获取许多示例和解释。规范为

      您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
      java.sql.*
      classes

      从哪里获得java.time类

      • 然后
      • 内置的
      • 标准JavaAPI的一部分,带有捆绑实现
      • Java9添加了一些次要功能和修复
      • 大部分java.time功能都在中向后移植到Java6和Java7
      • 更高版本的Android捆绑包实现了java.time类
      • 对于早期的Android(
        public List customizeDate(字符串月,整数年){
        列表=新的ArrayList();
        试一试{
        字符串edates=null;
        字符串sdates=null;
        如果(月份等于(“一月”)|月份等于(“三月”)||
        月份等于(“五月”)|月份等于(“七月”)||
        月份等于(“八月”)|月份等于(“十月”)||
        月份等于(“十二月”){
        字符串s1=“01”;
        字符串s2=“31”;
        字符串sdate=s1+“-”+月+“-”+年;
        系统输出打印项次(“起始日期”+sdate);
        SimpleDataFormat sd=新的SimpleDataFormat(“dd MMM yyyy”);
        日期ed=sd.parse(sdate);
        sdates=标准格式(ed);
        System.out.println(“ed”+ed+“--------------+sdates);
        字符串endDate=s2+“-”+月+“-”+年;
        系统输出打印项次(“结束日期”+结束日期);
        SimpleDataFormat s=新的SimpleDataFormat(“dd MMM yyyy”);
        日期d=s.parse(endDate);
        edates=s.格式(d);
        System.out.println(“d”+d+“--------------+edates);
        }如果(月等于(“四月”)|月等于(“六月”)||
        月份等于(“九月”)|月份等于(“十一月”)){
        字符串s3=“01”;
        字符串s4=“30”;
        字符串sdate=s3+“-”+月+“-”+年;
        系统输出打印项次(“起始日期”+sdate);
        SimpleDataFormat sd=新的SimpleDataFormat(“dd MMM yyyy”);
        日期ed=sd.parse(sdate);
        sdates=标准格式(ed);
        System.out.println(“ed”+ed+“--------------+sdates);
        字符串endDate=s4+“-”+月+“-”+年;
        系统输出打印项次(“结束日期”+结束日期);
        SimpleDataFormat s=新的SimpleDataFormat(“dd MMM yyyy”);
        日期d=s.parse(endDate);
        edates=s.格式(d);
        System.out.println(“d”+d+“--------------+edates);
        }否则{
        如果(第%4年==0和第%100年!=0 | |第%400年==0){
        System.out.println(“闰年”);
        如果(月份等于(“二月”)){
        字符串s5=“01”;
        字符串s6=“29”;
        字符串sdate=s5+“-”+月+“-”+年;
        系统输出打印项次(“起始日期”+sdate);
        SimpleDataFormat sd=
        新的简化格式(“dd-MMM-yyyy”);
        日期ed=sd.parse(sdate);
        sdates=标准格式(ed);
        System.out.println(“ed”+ed+“------------”+
        sdates);
        字符串endDate=s6+“-”+月+“-”+年;
        系统输出打印项次(“结束日期”+结束日期);
        简化格式=
        新的简化格式(“dd-MMM-yyyy”);
        
        ZoneId zoneId = ZoneId.of( "America/Montreal" );
        YearMonth yearMonthNow = YearMonth.now( zoneId );
        YearMonth yearMonthPrevious = yearMonthNow.minusMonths( 1 );
        LocalDate firstOfMonth = yearMonthPrevious.atDay( 1 );
        LocalDate lastOfMonth = yearMonthPrevious.atEndOfMonth();
        
        LocalDate today = LocalDate.now( DateTimeZone.forID( "Europe/Paris" ) );
        LocalDate firstOfThisMonth = today.withDayOfMonth( 1 );
        LocalDate firstOfLastMonth = firstOfThisMonth.minusMonths( 1 );
        LocalDate endOfLastMonth = firstOfThisMonth.minusDays( 1 );
        
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DATE, 1);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            Date lastDateOfPreviousMonth = cal.getTime();
            cal.set(Calendar.DATE, 1);
            Date firstDateOfPreviousMonth = cal.getTime();
        
            public List customizeDate(String month, int year) {
                List<String> list = new ArrayList<String>();
                try {
                    String edates = null;
                    String sdates = null;
                    if (month.equals("JAN") || month.equals("MAR") ||
                        month.equals("MAY") || month.equals("JUL") ||
                        month.equals("AUG") || month.equals("OCT") ||
                        month.equals("DEC")) {
                        String s1 = "01";
                        String s2 = "31";
        
                        String sdate = s1 + "-" + month + "-" + year;
                        System.out.println("Startdate" + sdate);
                        SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy");
                        Date ed = sd.parse(sdate);
                        sdates = sd.format(ed);
                        System.out.println("ed" + ed + "------------" + sdates);
                        String endDate = s2 + "-" + month + "-" + year;
                        System.out.println("EndDate" + endDate);
                        SimpleDateFormat s = new SimpleDateFormat("dd-MMM-yyyy");
                        Date d = s.parse(endDate);
                        edates = s.format(d);
                        System.out.println("d" + d + "------------" + edates);
                    } else if (month.equals("APR") || month.equals("JUN") ||
                               month.equals("SEP") || month.equals("NOV")) {
                        String s3 = "01";
                        String s4 = "30";
                        String sdate = s3 + "-" + month + "-" + year;
                        System.out.println("Startdate" + sdate);
                        SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy");
                        Date ed = sd.parse(sdate);
                        sdates = sd.format(ed);
                        System.out.println("ed" + ed + "------------" + sdates);
                        String endDate = s4 + "-" + month + "-" + year;
                        System.out.println("EndDate" + endDate);
                        SimpleDateFormat s = new SimpleDateFormat("dd-MMM-yyyy");
                        Date d = s.parse(endDate);
                        edates = s.format(d);
                        System.out.println("d" + d + "------------" + edates);
                    } else {
                        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                            System.out.println("Leap year");
                            if (month.equals("FEB")) {
                                String s5 = "01";
                                String s6 = "29";
                                String sdate = s5 + "-" + month + "-" + year;
                                System.out.println("Startdate" + sdate);
                                SimpleDateFormat sd =
                                    new SimpleDateFormat("dd-MMM-yyyy");
                                Date ed = sd.parse(sdate);
                                sdates = sd.format(ed);
                                System.out.println("ed" + ed + "------------" +
                                                   sdates);
                                String endDate = s6 + "-" + month + "-" + year;
                                System.out.println("EndDate" + endDate);
                                SimpleDateFormat s =
                                    new SimpleDateFormat("dd-MMM-yyyy");
                                Date d = s.parse(endDate);
                                edates = s.format(d);
                                System.out.println("d" + d + "------------" + edates);
                            }
                        } else {
                            System.out.println("Not a leap year");
                            String s7 = "01";
                            String s8 = "28";
                            String sdate = s7 + "-" + month + "-" + year;
                            System.out.println("Startdate" + sdate);
                            SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy");
                            Date ed = sd.parse(sdate);
                            sdates = sd.format(ed);
                            System.out.println("ed" + ed + "------------" + sdates);
                            String endDate = s8 + "-" + month + "-" + year;
                            System.out.println("EndDate" + endDate);
                            SimpleDateFormat s = new SimpleDateFormat("dd-MMM-yyyy");
                            Date d = s.parse(endDate);
                            edates = s.format(d);
                            System.out.println("d" + d + "------------" + edates);
                        }
                    }
                    list.add(edates);
                    list.add(sdates);
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                }
                return list;
            }
        }
        
        public static LocalDate getPreviousMonthStartDate() {
        
            return LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
        }
        
        public static LocalDate getPreviousMonthLastDate() {
        
            return LocalDate.now().minusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
        }
        
          DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy");
                        LocalDate now = LocalDate.now();
                        String startDate = now.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).format(format);
                        String endDate = now.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth()).format(format);