//指定格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String cureDate = format.format(new Date())
try {
//将字符串按指定格式转化为日期
Date date = format.parse(cureDate)
} catch (ParseException e) {
e.printStackTrace()
}
}
Date类型并没有格式,只有转换成String格式的时候让格式化显示。
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date())
Calendar calendar = Calendar.getInstance()
int year = Integer.parseInt(datetime.substring(0,4))
int month = Integer.parseInt(datetime.substring(5,7))
int date = Integer.parseInt(datetime.substring(8,10))
int hour = Integer.parseInt(datetime.substring(11,13))
int minute = Integer.parseInt(datetime.substring(14,16))
//int second = Integer.parseInt(datetime.substring(17,19))
if(calendar.get(Calendar.YEAR)>year){
int y = calendar.get(Calendar.YEAR)-year
扩展资料:
Date类可以在java.util包中找到,用一个long类型的值表示一个指定的时刻。它的一个有用的构造函数是Date(),创建一个表示创建时刻的对象。getTime()方法返回Date对象的long值。
import java.util.*
public class Now {
public static void main(String[] args) {
Date now = new Date()
long nowLong = now.getTime()
System.out.println("Value is " + nowLong)
参考资料来源:百度百科-java日期函数