zoukankan      html  css  js  c++  java
  • 日期处理

    //获得某月第一天 最后一天   上个月第一天  最后一天   本周第一个周一
    package
    DateDemo; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Demo05 { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.format(getTheFirstDayOfWeek(Calendar.getInstance()))); System.out.println(sdf.format(getTheFirstDayOfMonth(Calendar.getInstance()))); System.out.println(sdf.format(getTheLastOfMonth(Calendar.getInstance()))); System.out.println(sdf.format(getTheLastDayofTheLastMonth(Calendar.getInstance()))); System.out.println(sdf.format(getTheFirstDayofTheLastMonth(Calendar.getInstance()))); } public static Date getTheFirstDayOfWeek(Calendar c) { int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;//一周的第几天 if (day_of_week == 0){ day_of_week = 7; } c.add(Calendar.DATE, -day_of_week + 1); c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0); return c.getTime(); } public static Date getTheFirstDayOfMonth(Calendar c){//月的第一天 c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH)); c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0); return c.getTime(); } public static Date getTheLastOfMonth(Calendar c){//月的最后一天 c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 23, 59, 59); return c.getTime(); } public static Date getTheLastDayofTheLastMonth(Calendar c){//上个月最后一天 c.add(Calendar.MONTH, -1); return getTheLastOfMonth(c); } public static Date getTheFirstDayofTheLastMonth(Calendar c){//上个月最后一天 c.add(Calendar.MONTH, -1); return getTheFirstDayOfMonth(c); } }
  • 相关阅读:
    数位DP
    组合
    卢卡斯Lucas&扩展卢卡斯
    [HNOI2014]道路堵塞
    [模板]三维凸包(无讲解)
    [CF526G]Spiders Evil Plan
    [CCPC2019 ONLINE]H Fishing Master
    [CCPC2019 ONLINE]E huntian oy
    [CF1037H]Security
    [CF1037F]Maximum Reduction
  • 原文地址:https://www.cnblogs.com/chengpeng15/p/5937045.html
Copyright © 2011-2022 走看看