转载自作者:happy_fish 
  
 //日期和时间处理示例import java.util.*;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;public class TestDateTime
{
    public static void main(String[] argv)
    {
        long nCurrentTime = System.currentTimeMillis();
        java.util.Date utilDate = new java.util.Date(nCurrentTime);        //以下是用于数据库操作的日期和时间类
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());  //java.util.Date和java.sql.Date的转换
        java.sql.Time sqlTime = new java.sql.Time(utilDate.getTime());
        java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(nCurrentTime);        //Calendar用于获取指定的时间项(字段)
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(nCurrentTime);        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;   //注意:返回的月份是基于0的!
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);        System.out.println("Current date is: " + year + "年" + month + "月" + day + "日");
        System.out.println("Current time is: " + hour + "时" + minute + "分" + second + "秒");        //SimpleDateFormat用于格式化日期和时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String szDatetime1 = df.format(utilDate);
        System.out.println("Current datetime is: " + szDatetime1);        //以下计算两个日期值之间相差的时间
        java.util.Date date2 = new java.util.Date(nCurrentTime - 24600 * 1000);        long nMilliSeconds = utilDate.getTime() - date2.getTime();
        long nSeconds =  nMilliSeconds / 1000;  //把毫秒换算成秒
        double nDays = nSeconds / (24d * 60d * 60d);  //把秒换算成天        String szDatetime2 = df.format(date2);
        System.out.println("Time between " + szDatetime1 + " and " + szDatetime2 + " is " + nSeconds + " seconds (" + nDays + " days)");
    }

解决方案 »

  1.   

    呵呵,以前写的一个例子,希望对你有帮助:
    import java.text.SimpleDateFormat;
    import java.text.ParseException;
    import java.util.Date;/**
     *实现如何从字符中time得到时间Date对象只要SimpleDateFormate(String patter)中的每个字符与time的字符对应就行,比如:
    time:   "1981-07-09 10:30:15"      或: "19810709103015"
    patter: "yyyy-MM-dd hh:mm:ss"            "yyyyMMddhhmmss"*/
    public class DatetimeFormat{
    /**将一个表示时间的字符串转换成标准格式
    *@param strTime --一个表示时间的字符串
    *@return String ---标准格式的时间字符串
    */
    public static String Convert(String strTime){
    String format1 = "yy/MM/dd";          //设置格式
    String format2 = "yyyy/MM/dd";
    String rtnStr = "";

    try{
    SimpleDateFormat tf = new SimpleDateFormat(format1);
    Date date = tf.parse(strTime);//字符串转换成Date
    tf.applyPattern(format2);
    rtnStr = tf.format(date);     //Date转换成字符串
    }catch(NullPointerException e){
    System.out.println("the pattern is null.");
    }catch(IllegalArgumentException ex){
    System.out.println("the pattern is invalid.");
    }catch(ParseException e){
    System.out.println("time format error.");
    }

    return rtnStr;
    }

    public static void main(String[] args){
    String time1 = "20/8/02";
    String time2 = "19810709103015"; System.out.println(Convert("20/8/02"));
    System.out.println(Convert("2002/08/2"));
    }
    }
      

  2.   

    //日期转换成字符型
    Format fmt = new SimpleDateFormat ("yyyy年MM月dd日");
    String strDate = fmt.format (rs.getDate ("DATES"));//字符型转换成日期型
    SimpleDateFormat fmtDate = new SimpleDateFormat ("yyyy年MM月dd日");
    java.util.Date dte = fmtDate.parse("2004/08/01");http://community.csdn.net/Expert/topic/3281/3281760.xml?temp=.4909479