碰到一个很无解的问题,有一款软件,用的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

解决方案 »

  1.   

    补充:
      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
      

  2.   

    如何使用simpledateformat转换成0000-00-00 00:00:00?
      

  3.   

    System.out.println(sdf.format(begin));
      

  4.   

    能不能改数据库,改成timestamp?
      

  5.   

    我现在是从前台用java代码插入数据,没法用mysql函数~
      

  6.   


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    调用 sdf.format()方法,不是parse()
      

  7.   

    另外,你插入时,日期不要用DATE获得,用calendar看看.
      

  8.   


    转自:http://mxm910821.iteye.com/blog/1171571
      

  9.   

    你们都理解错我的意思了,我不是想把日期的格式调整为“0000-00-00 00:00:00”,而是就是想设置一个Date类型的变量,变量的初始值为0000-00-00 00:00:00
      

  10.   

    你这个问题是无解的,主要是你这个默认值本来就不合法。因为最后你要通过date存进去,而date中,0月其实是上一年的12月,而0日却是上一月的最后一天。
    所以00-00最后肯定变成11-30了,要么你调整数据库的类型,要么调整默认值吧
      

  11.   

    package com.hetaimall.core.util;import java.sql.Timestamp;
    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();
    }
    }这个日期工具类里面有你想要的各种日期格式转化方法
      

  12.   

    好吧,现在明白你的意思了.DATETIME类型:NOW()函数以'YYYY-MM-DD HH:MM:SS'返回当前的日期时间,可以直接存到DATETIME字段中。不支持使用系统默认值。DATE类型:CURDATE()以'YYYY-MM-DD'的格式返回今天的日期,可以直接存到DATE字段中。不支持使用系统默认值。TIME类型:CURTIME()以'HH:MM:SS'的格式返回当前的时间,可以直接存到TIME字段中。不支持使用系统默认值。要想达到你那样的,有难度.
      

  13.   

    问题都没有整明白就瞎回,抢婚呀?
    LZ,mysql中是datetime类型,请问是如何访问数据库的,如果有用框架,那这不是问题,
    在实体类中定义该字段属性的类型为 Timestamp ,
    还可以干脆将其定义为String 类型,啥事都好办。