Java gets the first day and last day of last week

Source: Internet
Author: User

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 ("Get last 1st period:" + tt.getyesterday ());
System.out.println ("Get last week Giges period:" + 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 ("Last Day of last week:" +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 ("The first day of last week:" +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;
}
}

Java gets the first day and last day of last week

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.