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日期函数
@return返回长时间格式 yyyy-MM-dd HH:mm:ss
*/ public static Date getSqlDate() {
Date sqlDate = new java.sql.Date(new Date().getTime())
return sqlDate }
/**
* 获取现在时间
*
* @return返回长时间格式 yyyy-MM-dd HH:mm:ss
*/ public static Date getNowDate() {
Date currentTime = new Date()
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String dateString = formatter.format(currentTime)
ParsePosition pos = new ParsePosition(8)
Date currentTime_2 = formatter.parse(dateString, pos)
return currentTime_2 }