DateFormate

 

package com.gene.util;

import java.util.*;
import java.text.*;  

public class DateFormat {
            
//string转Date public static Date StringToDate(String str){ str = str.substring(0, 10); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } date = java.sql.Date.valueOf(str); // 只保留日期部分,返回的是java.sql.Date 2007-01-18 return date; }
//Date转String  public static String DateToString(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String str = null; str = format.format(date); // 2007-01-18 return str; } //去除分秒时 public static Date DateToDate(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String str = null; str = format.format(date); // 2007-01-18 return StringToDate(str); } }

你可能感兴趣的:(java,sql)