java中如何从日期类型中获取月

java中如何从日期类型中获取月,第1张

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

}

下面这段代码演示了从日期到规定格式的字符串,在从规定格式的字符串到日期的 *** 作,希望有所帮助

public class DateTransfer {

public static void main(String[] args) {

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 日期格式

Date date = new Date();// 获取当前时间的的Date对象

Systemerrprintln(date);

String now = dfformat(date);// 将date转化为规定格式的字符串

Systemerrprintln(now);

Date newDate = new Date();// 新的Date对象

try {

newDate = dfparse(now);// 将字符串转化为Date类型

} catch (ParseException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

Systemerrprintln(newDate);// 打印验证

}

}

java获取一个时间的年月日代码及相关解释说明参考下面代码

package zhidao;

import javautilCalendar;

public class Test {

 public static void main(String[] args) {

  Calendar cal=CalendargetInstance();//使用日历类

  int year=calget(CalendarYEAR);//获取年份

  int month=calget(CalendarMONTH)+1;//获取月份,因为从0开始的,所以要加1

  int day=calget(CalendarDAY_OF_MONTH);//获取天

  Systemoutprintln("结果:"+year+"-"+month+"-"+day);

 }

}

import javatextSimpleDateFormat;

import javautilDate;

public class Test {

/

@param args

/

public static void main(String[] args) {

// String str="<font face='Arial,Serif',size='+2' color='red'";

Date date=new Date();

SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");

String str=dfformat(date);

String str1=strsubstring(4, 6);

Systemoutprintln(str1);

}

}

因为你的这一句 Calendar c = CalendargetInstance();是写在循环外面的,声明了c以后那c就是这一刻的时间,无论你再怎么循环还是只打印声明c时的时间。这段代码我没太看明白你要做什么,不过你想循环输出当前的时间可以这样写:

while(true){

Calendar c = CalendargetInstance();

int time = cget(CalendarSECOND);

Systemoutprintln(time);

}

把Calendar c = CalendargetInstance();写在循环里就是不停的循环获得当前时间。

在JAVA中获取当前时间的月份并转换成int型可以采用Calendar类提供的方法进行。

具体代码如下:

Calendar calendar=CalendargetInstance();

//获得当前时间的月份,月份从0开始所以结果要加1

int month=calendarget(CalendarMONTH)+1;

import javautilDate;

import javatextSimpleDateFormat;

public class NowString {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

Systemoutprintln(dfformat(new Date()));// new Date()为获取当前系统时间

}

}

比如今天是2009年3月24号,

那本月第一天就是: 今天的年 + 月 + 01

本月的最后一天就是:(本月第一天)加1月减1天

明白了?

日期的加减可以用类Calendar实现

本月的开始时间,就是年朋+01

结束时间,简单一点就是下月1号减1天

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

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

原文地址:https://54852.com/web/9524977.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存