I used to use date to intercept, but the history of all the scenes are suitable, or the efficiency can not meet, or splicing extra trouble. It's better to be able to use Java's methods to achieve this. Because the cultural differences between China and the West sometimes in the simple way we have to add some other to calculate, who let Java is the language of the West, I also want to use the development of domestic language, but do not know this life can see. On the code:
SimpleDateFormat simdf = new SimpleDateFormat ("MM month DD day"); Calendar cal = Calendar.getinstance (); System.out.println ("Present Time:" +simdf.format (Cal.gettime ()));//Get year, month, Day System.out.println ("Year:" +cal.get (CAL) respectively. year)); System.out.println ("Month:" + (Cal.get (Cal. Month) (+1));//The foreigner put January into 0, translated into the Chinese month to add 1system.out.println ("Day:" +cal.get (CAL). DATE)); Cal.set (Cal. Day_of_week, Cal. MONDAY); String Weekhand = Simdf.format (Cal.gettime ()); System.out.println ("Current Time week 1st:" +weekhand);//cal.set (Cal. Day_of_week, Cal. SUNDAY); This does not accord with the Chinese view of time, the foreigner set last week Sunday as the beginning of a week. Cal.set (Calendar.date, Cal.get (Cal. DATE) + 6); String weeklast = Simdf.format (Cal.gettime ()); System.out.println ("The current time is on the Sunday of the day:" +weeklast);
Run output:
Current time: July 06 Year: 2017 months: 7th: 6 Current Time Week 1st period: July 03 Current time weekday Sunday: July 09
Java gets the month and day of the current time and the Monday weekend date of the current week, respectively