如何比较简单的获得上个月的一年第一天和最后一天天

主题信息(必填)
主题描述(最多限制在50个字符)
申请人信息(必填)
申请信息已提交审核,请注意查收邮件,我们会尽快给您反馈。
如有疑问,请联系
CSDN &《程序员》研发主编,投稿&纠错等事宜请致邮
你只管努力,剩下的交给时光!
如今的编程是一场程序员和上帝的竞赛,程序员要开发出更大更好、傻瓜都会用到软件。而上帝在努力创造出更大更傻的傻瓜。目前为止,上帝是赢的。个人网站:。个人QQ群:、
个人大数据技术博客:博客分类:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取前月的第一天
cal_1=Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH, -1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
firstDay = format.format(cal_1.getTime());
System.out.println("-----1------firstDay:"+firstDay);
//获取前月的最后一天
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
lastDay = format.format(cale.getTime());
System.out.println("-----2------lastDay:"+lastDay);
//获取当前月第一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String first = format.format(c.getTime());
System.out.println("===============first:"+first);
//获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
System.out.println("===============last:"+last);
浏览 78931
浏览: 791148 次
来自: 厦门
oracleStr2Clob,为什么要用反射,直接调用不可以吗 ...
我把敏感的用*号代替啦,这样可以了吧,等你加一个号,等着租房子 ...
dream80 写道是的,这个主要就是二步验证的问题,很多人不 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'MySQL日期获取:本月第一天、本月最后一天、上月第一天、上月最后一天、下月第一天、下月最后一天..... - 一转身就是一辈子 - CSDN博客
MySQL日期获取:本月第一天、本月最后一天、上月第一天、上月最后一天、下月第一天、下月最后一天.....
interval的说明:
1、当函数使用时,即interval(),为比较函数,如:interval(10,1,3,5,7); 结果为4;
原理:10为被比较数,后面1,3,5,7为比较数,将后面四个依次与10比较,看后面数字组有多少个少于10,则返回其个数。前提是后面数字组为从小到大排列,否则返回结果0。
2、当关键词使用时,表示为设置时间间隔,常用在date_add()与date_sub()函数里,如:interval 1 day ,解释为将时间间隔设置为1天。
# 本月第一天
select date_add(curdate(), interval - day(curdate()) + 1 day);
# 本月最后一天
select last_day(curdate());
# 上月第一天
select date_add(curdate()-day(curdate())+1,interval -1 month);
# 上月最后一天
select last_day(date_sub(now(),interval 1 month));
# 下月第一天
select date_add(curdate()-day(curdate())+1,interval 1 month);
# 下月最后一天
select last_day(date_sub(now(),interval -1 month));
# 本月天数
select day(last_day(curdate()));
# 上月今天的当前日期
select date_sub(curdate(), interval 1 month);
# 上月今天的当前时间(时间戳)
select unix_timestamp(date_sub(now(),interval 1 month));
# 获取当前时间与上个月之间的天数
select datediff(curdate(), date_sub(curdate(), interval 1 month));
我的热门文章java获取上周第一天,上周最后一天,上月第一天,上月最后一天,当周第一天,当周最后一天,java获取当天日期等时间工具类
import java.io.PrintS import java.text.ParseP import .text.SimpleDateF import java.util.C import java.util.D import java.util.GregorianC
public class TimeTest { & private int weeks = 0;
& public static void main(String[] args) & { & & TimeTest tt = new TimeTest(); & & System.out.println(&获取昨天日期:& + tt.getyd()); & & System.out.println(&获取当天日期:& + tt.getNowTime(&yyyy-MM-dd&)); & & System.out.println(&获取本周一日期:& + tt.getMondayOFWeek()); & & System.out.println(&获取本周日的日期~:& + tt.getCurrentWeekday()); & & System.out.println(&获取上周一日期:& + tt.getPreviousWeekday()); & & System.out.println(&获取上周日日期:& + tt.getPreviousWeekSunday()); & & System.out.println(&获得相应周的周六的日期:& + tt.getNowTime(&yyyy-MM-dd&)); & & System.out.println(&获取本月第一天日期:& + tt.getFirstDayOfMonth()); & & System.out.println(&获取本月最后一天日期:& + tt.getDefaultDay()); & & System.out.println(&获取上月第一天日期:& + tt.getPreviousMonthFirst()); & & System.out.println(&获取上月最后一天的日期:& + tt.getPreviousMonthEnd()); & }
& public static String getWeek(String sdate) & { & & Date date = strToDate(sdate);
& & Calendar c = Calendar.getInstance();
& & c.setTime(date);
& & return new SimpleDateFormat(&EEEE&).format(c.getTime()); & }
& public static Date strToDate(String strDate) & { & & SimpleDateFormat formatter = new SimpleDateFormat(&yyyy-MM-dd&);
& & ParsePosition pos = new ParsePosition(0);
& & Date strtodate = formatter.parse(strDate, pos);
& public static long getDays(String date1, String date2) & { & & if ((date1 == null) || (date1.equals(&&))) & & { & & & return 0L; & & } & & if ((date2 == null) || (date2.equals(&&))) & & { & & & return 0L; & & }
& & SimpleDateFormat myFormatter = new SimpleDateFormat(&yyyy-MM-dd&);
& & Date date =
& & Date mydate = & & try & & { & & & date = myFormatter.parse(date1);
& & & mydate = myFormatter.parse(date2); & & } & & catch (Exception localException) & & { & & }
& & long day = (date.getTime() - mydate.getTime()) / L;
& public String getyd() & { & & Calendar cal = Calendar.getInstance(); & & cal.add(5, -1); & & String yesterday = new SimpleDateFormat(&yyyy-MM-dd &).format(cal.getTime()); & & & }
& public String getDefaultDay() & { & & String str = &&;
& & SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd&);
& & Calendar lastDate = Calendar.getInstance();
& & lastDate.set(5, 1);
& & lastDate.add(2, 1);
& & lastDate.add(5, -1);
& & str = sdf.format(lastDate.getTime());
& public String getPreviousMonthFirst() & { & & String str = &&;
& & SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd&);
& & Calendar lastDate = Calendar.getInstance();
& & lastDate.set(5, 1);
& & lastDate.add(2, -1);
& & str = sdf.format(lastDate.getTime());
& public String getFirstDayOfMonth() & { & & String str = &&;
& & SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd&);
& & Calendar lastDate = Calendar.getInstance();
& & lastDate.set(5, 1);
& & str = sdf.format(lastDate.getTime());
& public String getCurrentWeekday() & { & & this.weeks = 0;
& & int mondayPlus = getMondayPlus();
& & GregorianCalendar currentDate = new GregorianCalendar();
& & currentDate.add(5, mondayPlus + 6);
& & Date monday = currentDate.getTime();
& & SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd&);
& & String preMonday = sdf.format(monday);
& & return preM & }
& public String getNowTime(String dateformat) & { & & Date now = new Date();
& & SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);
& & String hehe = dateFormat.format(now);
& private int getMondayPlus() & { & & Calendar cd = Calendar.getInstance();
& & int dayOfWeek = cd.get(7) - 1;
& & if (dayOfWeek == 1) & & { & & & return 0; & & }
& & return (1 - dayOfWeek); & }
& public String getMondayOFWeek() & { & & this.weeks = 0;
& & int mondayPlus = getMondayPlus();
& & GregorianCalendar currentDate = new GregorianCalendar();
& & currentDate.add(5, mondayPlus);
& & Date monday = currentDate.getTime();
& & SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd&);
& & String preMonday = sdf.format(monday);
& & return preM & }
& public String getPreviousWeekSunday() & { & & this.weeks = 0;
& & this.weeks -= 1;
& & int mondayPlus = getMondayPlus();
& & GregorianCalendar currentDate = new GregorianCalendar();
& & currentDate.add(5, mondayPlus + this.weeks);
& & Date monday = currentDate.getTime(); & & SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd&); & & String preMonday = sdf.format(monday);
& & return preM & }
& public String getPreviousWeekday() & { & & this.weeks -= 1;
& & int mondayPlus = getMondayPlus();
& & GregorianCalendar currentDate = new GregorianCalendar();
& & currentDate.add(5, mondayPlus + 7 * this.weeks);
& & Date monday = currentDate.getTime();
& & SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd&);
& & String preMonday = sdf.format(monday);
& & return preM & }
& public String getPreviousMonthEnd() & { & & String str = &&; & & SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd&); & & Calendar lastDate = Calendar.getInstance(); & & lastDate.add(2, -1); & & lastDate.set(5, 1); & & lastDate.roll(5, -1); & & str = sdf.format(lastDate.getTime()); & & & } }
上编:下编:
本月热门的内容
1 2 3 4 5 6 7 8 9 10
最近更新的内容安全检查中...
请打开浏览器的javascript,然后刷新浏览器
< 浏览器安全检查中...
还剩 5 秒&

我要回帖

更多关于 oracle上个月最后一天 的文章

 

随机推荐