碰到一个很无解的问题,有一款软件,用的mysql数据库,里面有一个字段是datetime类型,非空,默认值为0000-00-00 00:00:00,映射到java的bean上是Date类型,问题在于,我现在如何从前台写入默认值的数据?如果这个字段不赋值吧,插入的时候会报错,因为no-null,如果赋值吧,java中的Date内容根本就无法定义0000-00-00 00:00:00这样的日期,最最早的日期也是02-01-01 00:00:00,怎么办?请高手指点~mysqljava
调试欢乐多
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date begin = sdf.parse("0000-00-00 00:00:00");
System.out.println(begin.toString());
控制台输出的是:Sun Nov 30 00:00:00 CST 2
很显然也不是0000-00-00 00:00:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
调用 sdf.format()方法,不是parse()
转自:http://mxm910821.iteye.com/blog/1171571
所以00-00最后肯定变成11-30了,要么你调整数据库的类型,要么调整默认值吧
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;/**
* 对日期做处理的工具类.
*
* @author robin
* @version 1.0
*/
public final class DateTime { static final int[] DAY_OF_YEAR = { 31, 59, 90, 120, 151, 181, 212, 243,
273, 304, 334, 365 }; /** Note: all day ranged from 1-31 all month ranged from 1-12 */ // minimum number of days in a month
static final int[] DAYS_IN_MONTH = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31 }; static final SimpleDateFormat SDF_DATE = new SimpleDateFormat("yyyy-MM-dd"); static final SimpleDateFormat SDF_DATENO = new SimpleDateFormat("yyyyMMdd"); static final SimpleDateFormat SDF_DATETIME = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
static final SimpleDateFormat SDF_DATE_TIME = new SimpleDateFormat(
"yyyyMMddHHmmss");
static final SimpleDateFormat SDF_LONGSTRING = new SimpleDateFormat(
"yyMMddHHmmssSSS"); static final SimpleDateFormat SDF_LONGDATETIME = new SimpleDateFormat(
"MMMM dd, yyyy, EEE. hh:mm a", Locale.US); static final SimpleDateFormat SDF_SHORTDATETIME = new SimpleDateFormat(
"yyyy-MM-dd HH:mm"); static final SimpleDateFormat SDF_TIME = new SimpleDateFormat("HH:mm:ss"); /**
* Return an adjusted java.util.Date
*
* @param d
* A java.util.Date used to set Calendar's time
* @param val
* A value to be set into Calendar
* @param field
* A key specifies which field will the value is set to
* @return an adjusted java.util.Date
*/
public static Date add(java.util.Date d, int val, int field) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(field, val); return cal.getTime();
} /**
* Return day of week
*
* @param dd
* Specifies a date's day
* @param mm
* Specifies a date's month
* @param yyyy
* Specifies a date's year
* @return day of week
*/
public static int dayOfWeek(int dd, int mm, int yyyy) {
if (mm < 3) {
mm += 13;
yyyy--;
} else {
mm++;
} return (((dd + (int) ((26 * mm) / 10) + yyyy + (int) (yyyy / 4))
- (int) (yyyy / 100) + (int) (yyyy / 400) + 5) % 7) + 1;
} /**
* Return day of year
*
* @param dd
* Specifies a date's day
* @param mm
* Specifies a date's month
* @param yyyy
* Specifies a date's year
* @return day of year
*/
public static int dayOfYear(int dd, int mm, int yyyy) {
if (mm == 1) {
return dd;
} return DAY_OF_YEAR[mm - 2] + (((mm > 2) && isLeapYear(yyyy)) ? 1 : 0)
+ dd;
} /**
* Return the number of days in a specified month and year mm is 1 to 12
* return ranged from 28 to 31
*
* @param mm
* Specifies a date's month
* @param yyyy
* Specifies a date's year
* @return the number of days in a specified month and year
*/
public static int daysInMonth(int mm, int yyyy) {
return DAYS_IN_MONTH[mm - 1]
+ (((mm == 2) && isLeapYear(yyyy)) ? 1 : 0);
} /**
* 格式化时间为yyyy-MM-dd.
*
* @param d
* Date.
* @return String
*/
public static Date toFormatDate(java.util.Date d)throws Exception {
String date=SDF_DATE.format(d);
return SDF_DATE.parse(date);
} /**
* 格式化时间为yyyy-MM-dd.
*
* @param d
* Date.
* @return String
*/
public static String formatDate(java.util.Date d) {
return d != null ? SDF_DATE.format(d) : "";
} /**
* 格式化时间为yyyyMMdd.
*
* @param d
* Date.
* @return String
*/
public static String formatDateNO(java.util.Date d) {
return d != null ? SDF_DATENO.format(d) : "";
} /**
* 格式化时间为yyyy-MM-dd HH:mm:ss.
*
* @param d
* Date.
* @return String
*/
public static String formatDateTime(java.util.Date d) {
return d != null ? SDF_DATETIME.format(d) : "";
} /**
* 格式化时间为yyyyMMddHHmmss
*
* @param d
* Date.
* @return String
*/
public static String formatTimeToString(java.util.Date d) {
return d != null ? SDF_DATE_TIME.format(d) : "";
} /**
* 格式化时间为yyMMddHHmmssSSS.
*
* @param d
* Date.
* @return String
*/
public static String formatLongString(java.util.Date d) {
return d != null ? SDF_LONGSTRING.format(d) : "";
} /**
* 格式化时间为MMMM dd, yyyy, EEE. hh:mm a.
*
* @param d
* Date.
* @return String
*/
public static String formatLongDateTime(java.util.Date d) {
return d != null ? SDF_LONGDATETIME.format(d) : "";
} /**
* 格式化时间为yyyy-MM-dd HH:mm.
*
* @param d
* Date.
* @return String
*/
public static String formatShortDateTime(java.util.Date d) {
return d != null ? SDF_SHORTDATETIME.format(d) : "";
} /**
* 格式化时间为HH:mm:ss.
*
* @param d
* Date.
* @return String
*/
public static String formatTime(java.util.Date d) {
return d != null ? SDF_TIME.format(d) : "";
} static Calendar getCalendar() {
return Calendar.getInstance();
} static Calendar getCalendar(int year, int month, int date, int hour,
int minute, int second, int millisecond) {
Calendar cal = getCalendar();
cal.set(year, month - 1, date, hour, minute, second);
cal.set(Calendar.MILLISECOND, millisecond); return cal;
} /**
* Return an java.util.Date of current time
*
* @return an java.util.Date of current time
*/
public static Date getDate() {
return getCalendar().getTime();
} /**
* Return an java.util.Date of a given time
*
* @param year
* the value used to set the YEAR time field.
* @param month
* the value used to set the MONTH time field
* @param date
* the value used to set the DATE time field.
* @return an java.util.Date of a given time
*/
public static Date getDate(int year, int month, int date) {
return getCalendar(year, month, date, 0, 0, 0, 0).getTime();
} /**
* Return an java.util.Date of a given time
*
* @param year
* the value used to set the YEAR time field.
* @param month
* the value used to set the MONTH time field
* @param date
* the value used to set the DATE time field.
* @param hour
* the value used to set the HOUR_OF_DAY time field.
* @param minute
* the value used to set the MINUTE time field.
* @param second
* the value used to set the SECOND time field.
* @return an java.util.Date of a given time
*/
public static Date getDate(int year, int month, int date, int hour,
int minute, int second) {
return getCalendar(year, month, date, hour, minute, second, 0)
.getTime();
}
}这个日期工具类里面有你想要的各种日期格式转化方法
LZ,mysql中是datetime类型,请问是如何访问数据库的,如果有用框架,那这不是问题,
在实体类中定义该字段属性的类型为 Timestamp ,
还可以干脆将其定义为String 类型,啥事都好办。