zoukankan      html  css  js  c++  java
  • 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
    		}
    		
    		
    	}
    }
    

      

  • 相关阅读:
    数据结构--实验5---排序(c)
    数据结构---实验4--查找(c)
    Tornado 模板(StaticFileHandler/static_path/template_path等) 笔记
    tornado请求头/状态码/接口 笔记
    Tornado 文件操作笔记
    tornado输入-get_query_argument()等 笔记
    tornado 笔记
    干货-递归下降分析器 笔记(具体看Python Cookbook, 3rd edition 的2.19章节)
    xml.sax 笔记
    HTMLParser 笔记
  • 原文地址:https://www.cnblogs.com/wujixing/p/5372047.html
Copyright © 2011-2022 走看看