时间:2021-07-01 10:21:17 帮助过:18人阅读
礼悟:
好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。
javaSE:8
os:windows7 x64
ide:MyEclipse 2017
代码
package com.jizuiku.jdbc;
/**
*
*
* @author 给最苦
* @version V17.11.08
*/
public class UtilDateToSqlDate {
public static void main(String[] args) {
java.util.Date uDate = new java.util.Date();
System.out.println(uDate);
// 进行转换
java.sql.Date sDate = new java.sql.Date(uDate.getTime());
System.out.println(sDate);
}
}
效果
util.date的toString方法的源代码
/**
* Converts this <code>Date</code> object to a <code>String</code>
* of the form:
* <blockquote><pre>
* dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
* where:<ul>
* <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
* Thu, Fri, Sat</tt>).
* <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
* Jul, Aug, Sep, Oct, Nov, Dec</tt>).
* <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
* <tt>31</tt>), as two decimal digits.
* <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
* <tt>23</tt>), as two decimal digits.
* <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
* <tt>59</tt>), as two decimal digits.
* <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
* <tt>61</tt>, as two decimal digits.
* <li><tt>zzz</tt> is the time zone (and may reflect daylight saving
* time). Standard time zone abbreviations include those
* recognized by the method <tt>parse</tt>. If time zone
* information is not available, then <tt>zzz</tt> is empty -
* that is, it consists of no characters at all.
* <li><tt>yyyy</tt> is the year, as four decimal digits.
* </ul>
*
* @return a string representation of this date.
* @see java.util.Date#toLocaleString()
* @see java.util.Date#toGMTString()
*/
public String toString() {
// "EEE MMM dd HH:mm:ss zzz yyyy";
BaseCalendar.Date date = normalize();
StringBuilder sb = new StringBuilder(28);
int index = date.getDayOfWeek();
if (index == BaseCalendar.SUNDAY) {
index = 8;
}
convertToAbbr(sb, wtb[index]).append(‘ ‘); // EEE
convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(‘ ‘); // MMM
CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(‘ ‘); // dd
CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(‘:‘); // HH
CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(‘:‘); // mm
CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(‘ ‘); // ss
TimeZone zi = date.getZone();
if (zi != null) {
sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz
} else {
sb.append("GMT");
}
sb.append(‘ ‘).append(date.getYear()); // yyyy
return sb.toString();
}
sql.date toString方法的源代码
/**
* Formats a date in the date escape format yyyy-mm-dd.
* <P>
* @return a String in yyyy-mm-dd format
*/
@SuppressWarnings("deprecation")
public String toString () {
int year = super.getYear() + 1900;
int month = super.getMonth() + 1;
int day = super.getDate();
char buf[] = "2000-00-00".toCharArray();
buf[0] = Character.forDigit(year/1000,10);
buf[1] = Character.forDigit((year/100)%10,10);
buf[2] = Character.forDigit((year/10)%10,10);
buf[3] = Character.forDigit(year%10,10);
buf[5] = Character.forDigit(month/10,10);
buf[6] = Character.forDigit(month%10,10);
buf[8] = Character.forDigit(day/10,10);
buf[9] = Character.forDigit(day%10,10);
return new String(buf);
}
java优秀,值得学习。
学习资源:源代码 + 清净的心地。
jdbc-mysql基础 util.date 和 sql.date 的toString方法是不同的
标签:new esc time escape rar sprintf .sql stat available