JAVA基础--日期处理

用SimpleDateFormat方法格式化日期格式:

package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TestDate {
	public static void main(String[] args) throws Exception{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		Connection conn =  DriverManager.getConnection("jdbc:mysql://109.110.100.56/tsdr_project?user=root");
		Statement stmt=conn.createStatement();
		ResultSet rs =  stmt.executeQuery("select distinct UpdateTime from defects_infor_daily");
		while(rs.next()){
			Date d = rs.getDate("UpdateTime");
			Timestamp ts=rs.getTimestamp("UpdateTime");
			//SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
			System.out.println(sdf.format(d));  //2016.04.11 公元 at 00:00:00 CST
			System.out.println(sdf.format(ts));  //2016.04.11 公元 at 07:30:35 CST
			Calendar c= Calendar.getInstance();			
			c.setTime(d);
			System.out.println(c.get(Calendar.MONTH));	 //3		
		}
		rs.close();
		stmt.close();
		conn.close();
	}
}

  

UTC:世界时间

GMT: 格林威治时间, 


 

java.util.Timezone

java.util.Calendar                          java.util.Date                                                                                    java.text.DateFormat  

java.util.GregrorianCalendar             java.sql.Date    java.sql.Time      java.sql.Timestamp                               java.text.SimpleDateFormat

 


 

package DAO;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;



public class TestDateAndTime {
	public static void main(String[] args) throws Exception{
		System.out.println(System.currentTimeMillis());   //1460360597062
		
		Date d = new Date();
		System.out.println(d);  //Mon Apr 11 15:43:17 CST 2016
		
		Calendar c = Calendar.getInstance();
		System.out.println(c.get(Calendar.YEAR));   //2016
		
		String s = "1970-12-30 08:24:37.0";
		Timestamp ts = Timestamp.valueOf(s);
		System.out.println(ts);     //1970-12-30 08:24:37.0
		
		Calendar cts = Calendar.getInstance();
		cts.setTime(ts);
		System.out.println(cts.get(Calendar.MONTH)+1);   //12
		
		Calendar cJapan = new GregorianCalendar(TimeZone.getTimeZone("Japan"));   //参数是日本的时区
		System.out.println(cJapan.get(Calendar.HOUR_OF_DAY));  //16  比中国晚1个小时
		
		for(String str:TimeZone.getAvailableIDs()){
			System.out.println(str);          //显示所有时区, 替代Japan
		}
		
		
	}
}

  

 

posted @ 2016-04-09 16:58  wujixing909  阅读(199)  评论(0编辑  收藏  举报