java怎么算出当月和上去年这个月的值怎么算

java怎么算出当月和上去年这个月的值怎么算,第1张

import javatextParseException;

import javatextSimpleDateFormat;

import javautilCalendar;

import javautilDate;

public class DateTime {

public static void main(String[] args) throws ParseException {

DateTime dt = new DateTime();

Date date = new SimpleDateFormat("yyyy-MM-dd")parse("2008-02-14");

date = dtlastDayOfMonth(date);

Systemoutprintln (new SimpleDateFormat("yyyy-MM-dd")format(date));

date = new SimpleDateFormat("yyyy-MM-dd")parse("2008-02-14");

date = dtlastDayOfMonth2(date);

Systemoutprintln (new SimpleDateFormat("yyyy-MM-dd")format(date));

date = new SimpleDateFormat("yyyy-MM-dd")parse("2008-02-14");

date = dtlastDayOfMonth3(date);

Systemoutprintln (new SimpleDateFormat("yyyy-MM-dd")format(date));

date = new SimpleDateFormat("yyyy-MM-dd")parse("2008-02-14");

date = dtlastDayOfMonth4(date);

Systemoutprintln (new SimpleDateFormat("yyyy-MM-dd")format(date));

}

public Date lastDayOfMonth(Date date) {

Calendar cal = CalendargetInstance();

calsetTime(date);

calset(CalendarDAY_OF_MONTH, 1);

calroll(CalendarDAY_OF_MONTH, -1);

return calgetTime();

}

public Date lastDayOfMonth2(Date date) {

Calendar cal = CalendargetInstance();

calsetTime(date);

int value = calgetActualMaximum(CalendarDAY_OF_MONTH);

calset(CalendarDAY_OF_MONTH, value);

return calgetTime();

}

public Date lastDayOfMonth3(Date date) {

Calendar cal = CalendargetInstance();

calsetTime(date);

calset(CalendarDAY_OF_MONTH, 1);

caladd(CalendarMONTH, 1);

caladd(CalendarDATE, -1);

return calgetTime();

}

public Date lastDayOfMonth4(Date date) {

Calendar cal = CalendargetInstance();

calsetTime(date);

do {

caladd(CalendarDATE, 1);

}

while (calget(CalendarDATE) != 1);

caladd(CalendarDATE, -1);

return calgetTime();

}

}

//将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;

}

/

  

  获取指定月份的日历信息

  

  @param year

             年

  @param month

             月

  @return

 /

public static int[] getMonthCalendar(int year, int month) {

Calendar cl = CalendargetInstance();

clset(year, month, 1);

int firstDay = clgetMinimum(CalendarDAY_OF_MONTH);

int lastDay = clgetMaximum(CalendarDAY_OF_MONTH);

int[] day = new int[lastDay];

for (int i = 0; i < lastDay; i++) {

day[i] =  i + firstDay;

}

return day;

}

以上就是关于java怎么算出当月和上去年这个月的值怎么算全部的内容,包括:java怎么算出当月和上去年这个月的值怎么算、java中如何从日期类型中获取月、java 怎么获取一个月的日期等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/10150870.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存