
1、Date date=new Date();这个是java提供的时间类,可以从中取出,年、月日、时、分、秒
2、SimpleDateFormat这个是时间格式类,对时间进行格式化
String time=new SimpleDateFormat("HH:mm:ss")format(new Date())
time=15:02:03
String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date())
time=2015-05-26 15:02:03
3、SystemcurrentTimeMillis(),返回的是long型日期时间
long time=SystemcurrentTimeMillis();
time=352632563256;
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()为获取当前系统时间
}
}
具体如下:
javautilCalendar
voidsetTime(Date date)
Sets this Calendar's time with the given Date设置日期时间
abstract void add(int field, int amount)
Adds or subtracts the specified amount of time to the given calendar field,
based on the calendar's rules
参数1是 CalendarDAY_OF_MONTH
参数2是天数,如果是负数,则为减
DategetTime()
Returns a Date object representing thisCalendar's time value (millisecond offset from the Epoch")得到时间
扩展资料
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等
(参考资料 百度百科 Java)
通过new Date获取当前的日期与时间
示例:
public static void main(String[] args){Date now = new Date(); //获取当前时间
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//格式化当前日期时间,显示如2015/06/27 14:22:22
}
用SystemcurrentTimeMillis()方法来实现 。
代码如下:
javasqlDate currentDate = new javasqlDate(SystemcurrentTimeMillis());说明:
返回long类型,一般用于获取某个方法或其它的执行时间差,在开始前获取一次,在结束时获取一次,结束时间减去开始时间,得到执行时间。
1Date() + SimpleDateFormat()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");Date date = new Date();Systemoutprintln(dateFormatformat(date));
2 Calender() + SimpleDateFormat()Calendar cal = CalendargetInstance();
Systemoutprintln(dateFormatformat(calgetTime()));
举个例子给你:
import javautilDate;
import javatextDateFormat;
import javatextSimpleDateFormat;
import javautilCalendar;
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
Systemoutprintln(dateFormatformat(date));
//get current date time with Calendar()
Calendar cal = CalendargetInstance();
Systemoutprintln(dateFormatformat(calgetTime()));
}
}
别忘了import
以上就是关于如何用java语言 获得系统当前日期全部的内容,包括:如何用java语言 获得系统当前日期、java如何用函数获取当前的系统时间、java 获取当前时间,将天数减一天,返回Date.等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)