zoukankan      html  css  js  c++  java
  • java获取上个星期第一天和最后一天

    package com.goldcn.jzgmanageplat.b2b.controller;

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;

    import com.goldcn.common.utils.CommonUtils;

    public class aa {
     public static void main(String[] args) throws Exception{
      aa tt = new aa();
      System.out.println("获取上周一日期:" + tt.getYesterday());
      System.out.println("获取上周日日期:" + tt.getPreviousWeekSunday());
      
     }

     public static int getMondayPlus() {
      Calendar cd = Calendar.getInstance();

      int dayOfWeek = cd.get(7) - 1;

      if (dayOfWeek == 1) {
       return 0;
      }

      return (1 - dayOfWeek);
     }

     public String getPreviousWeekSunday() {

      int weeks =- 1;

      int mondayPlus = getMondayPlus();

      GregorianCalendar currentDate = new GregorianCalendar();

      currentDate.add(5, mondayPlus + weeks);
      

      Date monday = currentDate.getTime();
      
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      String preMonday = sdf.format(monday);
      
      //System.out.println("上周最后一天:"+CommonUtils.parseDateToLong(preMonday+" 23:59:59", "yyyy-MM-dd hh:mm:ss"));

      return preMonday;
     }

     public String getPreviousWeekday() throws Exception {
      int week=-1;

      int mondayPlus = getMondayPlus();

      GregorianCalendar currentDate = new GregorianCalendar();

      currentDate.add(5, mondayPlus + 7 * week);

      Date monday = currentDate.getTime();
      
      
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

      String preMonday = sdf.format(monday);
      
      
      //System.out.println("上周第一天:"+CommonUtils.parseDateToLong(preMonday+" 00:00:00", "yyyy-MM-dd hh:mm:ss"));
      
      
      return preMonday;
     }

     public String getYesterday(){
      Calendar cal=Calendar.getInstance();
            cal.add(Calendar.DATE,-1);
            Date time=cal.getTime();
           
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");

      String yesterday = sdf.format(time);
      
      return yesterday;
     }
    }

  • 相关阅读:
    知识点:Mysql 索引优化实战(3)
    知识点:Mysql 索引原理完全手册(2)
    知识点:Mysql 索引原理完全手册(1)
    大数据体系:数据分析体系总图
    数据化分析:微信文章不增粉的主要原因
    提问:MicrosoftUnderlying input stream returned zero bytes
    优化:更优雅的异步代码?
    涨姿势:Mysql 性能优化完全手册
    总结:Java 集合进阶精讲1
    冷知识点:COLLATE 关键字是什么意思?
  • 原文地址:https://www.cnblogs.com/cuijj/p/4561805.html
Copyright © 2011-2022 走看看