可以通过GregorianCalendar 函数来实现日期定义获取到本月的下一个月,通过”calendaradd(CalendarMONTH, -2)“获取上个月的信息(上月和下个月是差2)
GregorianCalendar calendar = new GregorianCalendar(2015, 6, 28);//灵活的输入年份,月份,日期,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//定义日期显示格式
Systemoutprintln(sdfformat(calendargetTime()));//打印当前月份的下一个月份
for (int i = 0; i < 1; i++) {
calendaradd(CalendarMONTH, -2);//获取上个月月份
Systemoutprintln(sdfformat(calendargetTime()));//输出结果
}
结果:
//将yyyyMMdd转为date
public static Date getCoreToEmsDateStr(String dateStr){
DateFormat format = new SimpleDateFormat("yyyyMMdd");
Date d = null;
try{
d = formatparse(dateStr);
}catch(ParseException e){
eprintStackTrace();
}
return d;
}
public static String getDateAfterDays(Timestamp s,int days){
Timestamp currTimestamp = s;
for (int i=0;i<days;i++){
currTimestamp = getNextDate(currTimestamp);
}
return getDateTimeStr(currTimestamp,"3");
}
public static Timestamp getNextDate(javasqlTimestamp tsDate){
if(tsDate==null)
return null;
javautilCalendar calendar = CalendargetInstance();
calendarsetTime(tsDate);
return getDateTime(calendarget(CalendarYEAR),calendarget(CalendarMONTH)+1,calendarget(CalendarDATE)+1,
calendarget(CalendarHOUR_OF_DAY),calendarget(CalendarMINUTE),calendarget(CalendarSECOND));
}
public static javasqlTimestamp getDateTime(int year,int month,int day,int hour,int minute,int second){
javasqlTimestamp ts = null;
javautilDate dt = null;
javautilCalendar calendar = CalendargetInstance();
calendarclear();
calendarset(year,month-1,day,hour,minute,second);
dt = calendargetTime();
ts = new javasqlTimestamp(dtgetTime());
return ts;
}
/
比较两个时间是否相同
@param tsBeginDate
@param tsEndDate
@param bool
@return
/
public static long getDateInterval(Timestamp tsBeginDate,Timestamp tsEndDate,boolean bool){
long lDays = 0;
if(bool){
tsBeginDate = CommongetDateTime(CommongetDateString(tsBeginDate),bool);
}
if(tsBeginDate!=null&&tsEndDate!=null){
Log4jinfo("tsEndDategetTime ()===="+tsEndDate);
Log4jinfo("tsBeginDategetTime ()===="+tsBeginDate);
lDays = (tsEndDategetTime()-tsBeginDategetTime())/86400000+1;
Log4jinfo("lDays===="+lDays);
}
return lDays;
}
/
格式化成Timestamp类型
@param sDt
@param bool
@return
/
public static javasqlTimestamp getDateTime(String sDt,boolean bool){
try{
return javasqlTimestampvalueOf(sDt); //sDt format:yyyy-mm-dd hh:mm:ssfffffffff
}catch(IllegalArgumentException iae){
if(bool)
sDt = sDt+" 23:59:590";
else
sDt = sDt+" 00:00:000";
return javasqlTimestampvalueOf(sDt);
}
}
/
根据时间获取日期字符串
@param ts
@return
/
public static String getDateString(Timestamp ts){
if(ts==null)
return "";
Calendar calendar = CalendargetInstance();
calendarsetTime(ts);
String strMonth = StringvalueOf(calendarget(CalendarMONTH)+1);
if(strMonthlength()==1){
strMonth = "0"+strMonth;
}
String strDay = StringvalueOf(calendarget(CalendarDATE));
if(strDaylength()==1){
strDay = "0"+strDay;
}
return calendarget(CalendarYEAR)+"-"+strMonth+"-"+strDay;
}
以上就是关于java如何获取当天的上一个月全部的内容,包括:java如何获取当天的上一个月、java中如何从日期类型中获取月、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)