java中把时间转换成yyyy-MM-dd

java util下的Date类型转换成sql下的Date类型
2024-12-26 02:19:55
推荐回答(4个)
回答1:

写字符串呗 以前有个项目写过 下面是部分代码
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;
return y+"年前";
}else if((calendar.get(Calendar.MONTH)+1)>month){
int m = (calendar.get(Calendar.MONTH)+1)-month;
return m+"个月前";
}else if(calendar.get(Calendar.DAY_OF_MONTH)>date){
int d = calendar.get(Calendar.DAY_OF_MONTH)-date;
return d+"天前";
}else if(calendar.get(Calendar.HOUR_OF_DAY)>hour){
int h = calendar.get(Calendar.HOUR_OF_DAY)-hour;
return h+"小时前";
}else if(calendar.get(Calendar.MINUTE)>minute){
int s = calendar.get(Calendar.MINUTE)-minute;
return s+"分钟前";
}else{
return "刚刚";
}

回答2:

new java.sql.Date(new java.util.Date().getTime())

回答3:

new SimpleDateFormat("yyyy-MM-dd").format(new Date())

回答4:

邮箱发下