java中取得时间

java中取得时间,第1张

下面是我写的一个类,主要就是使用SimpleDateFormat将取得的时间(我取得的是默认当前时间,具体的你可以改)进行格式化输出已经根据你要的格式进行了修改,精确到小时

import javautil;

public class DateUtil{

public static String getDateString() {

SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHH");

return fmtformat(new Date());

}

}

常用的格式为:yyyy-MM-dd HH:mm:ss,分别对应年月日时分秒,你自己改吧

可以使用java中的日期格式化工具javatextSimpleDateFormat来格式化,以下为示例代码:

1创建javatextSimpleDateFormat实例

javatextSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", LocalegetDefault());

参数说明,"yyyy-MM-dd"表示日期的格式,也可以是这样的 yyyy-MM-dd HH:mm:ss注意大小写

2将Date对象转成字符串

Date date = new Date();

String str = sdfformat(date);

Systemoutprintln(str);

3将字符串转成Date

String str = "2013-12-12";

Date date = sdfparse(str);

Systemoutprinltn(date);

注意,SimpleDateFormat解析工具,指定的format以后,只能解析指定格式的日期,例如指定了yyyy-MM-dd则不可以再去解析 yyyy-MM-dd HH:mm:ss格式。

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

}

//得到long类型当前时间

long l = SystemcurrentTimeMillis();

//new日期对象

Date date = new Date(l);

//转换提日期输出格式

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Systemoutprintln(dateFormatformat(date));

以上就是关于java中取得时间全部的内容,包括:java中取得时间、java获取的日期只显示年月日不显示时分秒,怎么处理、java中如何从日期类型中获取月等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存