licheng700

BlogJava 首页 新随笔 联系 聚合 管理
  26 Posts :: 5 Stories :: 5 Comments :: 1 Trackbacks

package dateandtime;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SqlDateAndUtilDate {

 public static void main(String[] args) throws ParseException
 {
  java.sql.Date sqlDate=null;
  Date utilDate=null;
  utilDate= new Date(System.currentTimeMillis());
  System.out.println("utileDate="+utilDate);
  sqlDate=new java.sql.Date(System.currentTimeMillis());
  System.out.println("sqlDate="+sqlDate);
  System.out.println("sqlDateConvertoUtilDate="+((Date)sqlDate));
  Time sqlTime = new java.sql.Time(utilDate.getTime());
  System.out.println("sqlTime="+sqlTime);
        java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(utilDate.getTime());
        System.out.println("sqlTimestamp="+sqlTimestamp);
        String dataTime="3/Jun/2005 02:12:10 AM";
        SimpleDateFormat format = new SimpleDateFormat(
                "dd/MMM/yyyy hh:mm:ss a", Locale.ENGLISH);
        Date date=format.parse(dataTime);
        System.out.println(date);
        /*format=new  SimpleDateFormat(
                "yyyy-MMM-dd hh:mm:ss", Locale.ENGLISH);*/
        format.applyPattern("yyyy-MMM-dd hh:mm:ss");
        String dateString="2005-Aug-04 11:06:00";
        date=format.parse(dateString);
        System.out.println(date);
 }
}


package dateandtime;

//日期和时间处理示例

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)");
    }
}


 

posted on 2005-08-04 11:27 小海船 阅读(824) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: