java日期转换

java.sql.Date、java.util.Date和字符串转换

import java.text.ParseException; import java.text.SimpleDateFormat; public class DateUtil { //String --> java.util.Date public static java.util.Date getDateTime(String date_string) throws ParseException{ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.parse(date_string); } //java.util.Date --> String public static String getFormatDate(java.util.Date date){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return simpleDateFormat.format(date); } //java.util.Date --> java.sql.Date public static java.sql.Date toSqlDate(java.util.Date date){ return new java.sql.Date(date.getTime()); } //java.util.Date --> java.sql.Timestamp public static java.sql.Timestamp toSqlDateTime(java.util.Date date){ return new java.sql.Timestamp(date.getTime()); } public static void main(String args[]) throws ParseException{ System.out.println(getDateTime("2009-12-20 13:30:58")); System.out.println(getFormatDate(new java.util.Date())); System.out.println(toSqlDate(new java.util.Date())); System.out.println(toSqlDateTime(new java.util.Date())); } }

你可能感兴趣的:(java日期转换)