本文主要是介绍获取本周一,本周日,本月头,月尾,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import java.text.SimpleDateFormat;
import java.util.Calendar;public class DateTest {public static void main(String[] args){firstDayOfWeek();lasyDayOfWeek();nextMondayOfWeek();firstdayOfMonth();lastdayOfMonth();}/*** 周一*/public static void firstDayOfWeek(){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar cal = Calendar.getInstance();cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //获取本周一的日期System.out.println(simpleDateFormat.format(cal.getTime()));}/*** 周日*/public static void lasyDayOfWeek(){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar cal = Calendar.getInstance();cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);cal.add(Calendar.WEEK_OF_YEAR, 1);System.out.println(simpleDateFormat.format(cal.getTime()));}/*** 下个星期一*/public static void nextMondayOfWeek(){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar cal = Calendar.getInstance();cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);cal.add(Calendar.WEEK_OF_YEAR, 1);System.out.println(simpleDateFormat.format(cal.getTime()));}/*** 月头*/public static void firstdayOfMonth(){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar cal = Calendar.getInstance();cal.set(Calendar.DATE, cal.getActualMinimum(Calendar.DATE)); System.out.println(simpleDateFormat.format(cal.getTime()));}/*** 月尾*/public static void lastdayOfMonth(){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar cal = Calendar.getInstance();cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE)); System.out.println(simpleDateFormat.format(cal.getTime()));}}
这篇关于获取本周一,本周日,本月头,月尾的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!