Java中Date的一些操作

一、Java与数据库的日期格式问题
util包下的Date和sql包包下面的两个Date

		Date date = new Date();
        java.sql.Date time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            time = (java.sql.Date) sdf.parse(String.valueOf(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

二、把时间从string转成long型,在由long转成Date

SimpleDataFormat sdf   =  new SimpleDateFormat("yyyy-MM-dd");
	Date date = sdf.parse("2015-9-9");
	long longTime = date.getTime();
	Date getDate = new Date(longTime);

三、通过System类的currentTimeMillis()方法获得

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(System.currentTimeMillis());

四、通过Calendar获取

Calendar cal=Calendar.getInstance();      
int y=cal.get(Calendar.YEAR);   //年   
int m=cal.get(Calendar.MONTH)+1;   //月,比当前月少1   
int d=cal.get(Calendar.DATE);    //日
int w = cal.get(Calendar.DAY_OF_WEEK);//星期 周日 -- 周一
int h=cal.get(Calendar.HOUR_OF_DAY); //时     
int mi=cal.get(Calendar.MINUTE);      //分
int s=cal.get(Calendar.SECOND);      //秒
发布了27 篇原创文章 · 获赞 19 · 访问量 1291

猜你喜欢

转载自blog.csdn.net/weixin_42804692/article/details/103396015
今日推荐