Java returns the Sunday of a day and the Sunday of the current week

import java. text. ParseException; import java. text. SimpleDateFormat; import java. util. Calendar; import java. util. Date; import java. util. GregorianCalendar; Public class test {public static void main (string [] args) {calendar cal = calendar. Getinstance(); simpledateformat DF = new simpledateformat ("yyyy MM DD"); cal. Set (calendar. Day_of_week, calendar. Monday); / / get the date of this Monday system.out.println (DF. Format (CAL. Gettime()) )); // This output is the date of last Sunday, because foreigners regard Sunday as the first day set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY); // Adding one week is the date of this Sunday as we Chinese understand it add(Calendar.WEEK_OF_YEAR,1); System. out. println(df.format(cal.getTime())); String str="2013-11-13 00:10:00"; SimpleDateFormat ddd=new SimpleDateFormat("dd"); System. out. println( getLastDayOfWeek(str) ); } // Return Sunday of a day public static string getlastdayofweek (string string) {string STR = string; simpledateformat SDF = new simpledateformat ("yyyy MM DD HH: mm: SS"); date date = null; try {date = sdf.parse (str.tostring());} catch (ParseException e) { e.printStackTrace(); } Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK,c.getFirstDayOfWeek() + 6); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String s = format. format(c.getTime()); return s; }

}

result:

2013-11-11 2013-11-17 2013-11-17 12:10:00

In addition, get the start time of Monday and the start time of Sunday

//Return Sunday of a day public static string getlastdayofweek (string) {string STR = string; simpledateformat SDF = new simpledateformat ("yyyy MM DD"); date date = null; try {date = SDF. Parse (str.tostring());} catch (ParseException e) { e.printStackTrace(); } Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK,c.getFirstDayOfWeek() + 6); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String s = format. format(c.getTime()); s = s + " 23:59:59"; return s; } // Return Monday of a day public static string getfirstdayofweek (string string) {string STR = string; simpledateformat SDF = new simpledateformat ("yyyy MM DD"); date date = null; try {date = sdf.parse (str.tostring());} catch (ParseException e) { e.printStackTrace(); } Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK,c.getFirstDayOfWeek()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String s = format. format(c.getTime()); s = s + " 00:00:00"; return s; }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>