java中获得当前时间(yyyy-mm-dd)

java中获得当前时间(yyyy-mm-dd),第1张

import javatextSimpleDateFormat;

import javautilDate;

Date d=new Date();//获取时间

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//转换格式

Systemoutprintln(sdfformat(d));//打印

时间戳通常是”yyyyMMddHHmmss“的,举例:

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

String str = sdfformat(date);

输出结果:20150704173752。

备注:时间戳中的时间显示格式可以根据实际情况设置即可。

/

获取当前系统时间

@return 返回短时间字符串格式yyyy-MM-dd

/

public static String getStringDateShort() {

Date currentTime = new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

String dateString = formatterformat(currentTime);

return dateString;

}

获取当前系统时间

@return返回短时间格式 yyyy-MM-dd

/

public static Date getNowDateShort() {

Date currentTime = new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

String dateString = formatterformat(currentTime);

ParsePosition pos = new ParsePosition(8);

Date currentTime_2 = formatterparse(dateString, pos);

return currentTime_2;

}

用SystemcurrentTimeMillis()方法来实现

代码如下:

javasqlDate currentDate = new javasqlDate(SystemcurrentTimeMillis());说明:

返回long类型,一般用于获取某个方法或其它的执行时间差,在开始前获取一次,在结束时获取一次,结束时间减去开始时间,得到执行时间。

java在当前系统时间加一天主要是使用calendar类的add方法,如下代码:import javautilCalendar;import javautilDate;public class ceshi { public static void main(String[] args) { Date date = new Date();// 新建此时的的系统时间 Systemoutprintln(getNextDay(date));// 返回明天的时间 } public static Date getNextDay(Date date) { Calendar calendar = CalendargetInstance(); calendarsetTime(date); calendaradd(CalendarDAY_OF_MONTH, +1);//+1今天的时间加一天 date = calendargetTime(); return date; }}运行结果:

以上就是关于java中获得当前时间(yyyy-mm-dd)全部的内容,包括:java中获得当前时间(yyyy-mm-dd)、java 如何获取当前时间的时间戳、java 如何获取当前系统时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存