日期处理
Posted 鹏鹏进阶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了日期处理相关的知识,希望对你有一定的参考价值。
//获得某月第一天 最后一天 上个月第一天 最后一天 本周第一个周一
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); } }
以上是关于日期处理的主要内容,如果未能解决你的问题,请参考以下文章
sql 日期转换代码片段 - Dato,120,konvertere
如何从片段中调用 getSupportFragmentManager()?
vscode 用户代码片段 vue初始化模板 Snippet #新加入开头注释 自动生成文件名 开发日期时间等内容