Show the algorithm for one week

Source: Internet
Author: User
Tags dateformat


This is how the requirement is, showing the calendar for a week.


public class Test {


public static void Main (string[] args) {
TODO auto-generated Method Stub


String number[] = new STRING[7];


Calendar calendar = Calendar.getinstance ();
Date date = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");
String Currentday = Dateformat.format (date);


int currentdayofweek = Calendar.get (Calendar.day_of_week);

System.out.println ("Currentdayofweek =" +currentdayofweek);


Number[currentdayofweek-1] = currentday.substring (
Currentday.length ()-2, Currentday.length ());


int beforedaybetween=currentdayofweek-1;

int k=1;
int Y=1;

if (beforedaybetween>0) {

for (int i=beforedaybetween-1;i>=0;i--) {
String Stime=getdayoffset (currentday,-k);
Number[i]=stime.substring (Stime.length ()-2, Stime.length ());
k++;
}

if (beforedaybetween<6) {
for (int i=currentdayofweek;i<=6;i++) {
String Stime=getdayoffset (currentday,y);
Number[i]=stime.substring (Stime.length ()-2, Stime.length ());
y++;
}
}
}else {
for (int i=0;i<7;i++) {
String Stime=getdayoffset (currentday,i);
Number[i]=stime.substring (Stime.length ()-2, Stime.length ());
}
}

for (int j=0;j<number.length;j++) {
System.out.println ("number[j] =" +number[j]);
}


}


public static string Getdayoffset (String Sourcedatetime,int dayoffset) {
Calendar mcalendar=calendar.getinstance ();
Mcalendar.settime (Converttodate (sourcedatetime));
Mcalendar.add (Calendar.day_of_month, Dayoffset);
SimpleDateFormat sf=new SimpleDateFormat ("Yyyy-mm-dd");
Return Sf.format (Mcalendar.gettime ());
}
public static Date converttodate (String datestr) {
Date Mdate=null;
try {
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");
Mdate=dateformat.parse (DATESTR);
} catch (ParseException e) {
E.printstacktrace ();
}
return mdate;
}


}

Show the algorithm for one 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.