zoukankan      html  css  js  c++  java
  • java timer / date

    CST和GMT时间的区别

    http://www.cnblogs.com/sanshi/archive/2009/08/28/1555717.html

    今天遇到一个奇怪的问题,在服务器端通过 Java 获取当前时间为 Fri Aug 28 09:37:46 CST 2009, 转化为GMT时间为:28 Aug 2009 01:37:46 GMT,也就是说GMT时间加上 8 个小时等于CST表示的时间, 那这个CST不就是北京时间么,因为我们是在东八区的。

    一切看起来很正常,不过在客户端用JavaScript解析这个时间就有问题了:

    1. // Fri Aug 28 2009 23:37:46 GMT+0800
    2. new Date( 'Fri Aug 28 09:37:46 CST 2009' ).toString();


    好奇怪,这次GMT和CST表示的时间居然相差整整 14 个小时?



    百度一下

    找到这篇文章 ,问题已经很明了。

    GMT(Greenwich Mean Time)代表格林尼治标准时间,这个大家都知道。
    而CST却同时可以代表如下 4 个不同的时区:

    • Central Standard Time (USA) UT-6:00
    • Central Standard Time (Australia) UT+9:30
    • China Standard Time UT+8:00
    • Cuba Standard Time UT-4:00


    可见,CST可以同时表示美国,澳大利亚,中国,古巴四个国家的标准时间。

    前面提到的通过 Java 获取的CST时间用的是China Standard Time,而客户端JavaScript则默认采用的是美国的中部时间。

    所以将 Fri Aug 28 09:37:46 CST 2009 加上 6 个小时,再加上 8 个小时,就等于 Fri Aug 28 2009 23:37:46 GMT+0800

    可见,在以后的编程中为了避免错误,还是不要使用CST时间,而尽量采用GMT时间。

    http://guoqinhua1986-126-com.iteye.com/blog/231244

    ********************定时执行任务的三种方法***********
    1)java.util.Timer
    这个方法应该是最常用的,不过这个方法需要手工启动你的任务:
    Timer timer=new Timer();
    timer.schedule(new ListByDayTimerTask(),10000,86400000);
    这里的ListByDayTimerTask类必须extends TimerTask里面的run()方法。
    2)ServletContextListener
    这个方法在web容器环境比较方便,这样,在web server启动后就可以
    自动运行该任务,不需要手工操作。
    将ListByDayListener implements ServletContextListener接口,在
    contextInitialized方法中加入启动Timer的代码,在contextDestroyed
    方法中加入cancel该Timer的代码;然后在web.xml中,加入listener:
    < listener>
    < listener-class>com.qq.customer.ListByDayListener< /listener-class>
    < /listener>
    3)org.springframework.scheduling.timer.ScheduledTimerTask
    如果你用spring,那么你不需要写Timer类了,在schedulingContext-timer
    .xml中加入下面的内容就可以了:
    < ?xml version="1.0" encoding="UTF-8"?>
    < !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    < beans>
    < bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
    < property name="scheduledTimerTasks">
    < list>
    < ref local="MyTimeTask1"/>
    < /list>
    < /property>
    < /bean>
    < bean id="MyTimeTask" class="com.qq.timer.ListByDayTimerTask"/>
    < bean id="MyTimeTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    < property name="timerTask">
    < ref bean="MyTimeTask"/>
    < /property>
    < property name="delay">
    < value>10000< /value>
    < /property>
    < property name="period">
    < value>86400000< /value>
    < /property>
    < /bean>
    < /beans>

    1)java.util.Timer 这个方法应该是最常用的,不过这个方法需要手工启动你的任务: Timer timer=new Timer(); timer.schedule(new ListByDayTimerTask(),10000,86400000); 这里的ListByDayTimerTask类必须extends TimerTask里面的run()方法。
    2)ServletContextListener 这个方法在web容器环境比较方便,这样,在web server启动后就可以自动运行该任务,不需要手工操作。将ListByDayListener implements ServletContextListener接口,在 contextInitialized方法中加入启动Timer的代码,在contextDestroyed 方法中加入cancel该Timer的代码;然后在web.xml中,加入listener: < listener> < listener-class>com.qq.customer.ListByDayListener< /listener-class> < /listener> 3)org.springframework.scheduling.timer.ScheduledTimerTask 如果你用spring,那么你不需要写Timer类了,在schedulingContext-timer .xml中加入下面的内容就可以了: < ?xml version="1.0" encoding="UTF-8"?> < !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> < beans> < bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean"> < property name="scheduledTimerTasks"> < list> < ref local="MyTimeTask1"/> < /list> < /property> < /bean> < bean id="MyTimeTask" class="com.qq.timer.ListByDayTimerTask"/> < bean id="MyTimeTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask"> < property name="timerTask"> < ref bean="MyTimeTask"/> < /property> < property name="delay"> < value>10000< /value> < /property> < property name="period"> < value>86400000< /value> < /property> < /bean> < /beans>


    java 字符串<==>时间 格式转换

    http://virgos.iteye.com/blog/197894

    如:有这样一个字符串:“20070911121547”, 转换成时间格式:2007-09-11 12:15:47

    Java代码 复制代码
    1. public   class  bb {  
    2.     public   static   void  main(String[] args) {  
    3.         // TODO Auto-generated method stub       
    4.         SimpleDateFormat df = new  SimpleDateFormat( "yyyyMMddhhmmss" );  
    5.         String dateString = "20071128175545" ;  
    6.         try  {  
    7.             Date date = df.parse(dateString);  
    8.             System.out.println(df.format(date));  
    9.         } catch  (Exception ex) {  
    10.             System.out.println(ex.getMessage());  
    11.         }  
    12.     }  
    13.   
    14. }  
    public class bb {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub    
    		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
    		String dateString = "20071128175545";
    		try {
    			Date date = df.parse(dateString);
    			System.out.println(df.format(date));
    		} catch (Exception ex) {
    			System.out.println(ex.getMessage());
    		}
    	}
    
    }



    时间无非就是字符串类型转向时间类型,或则时间类型转向字符串类型,还有就是前一个时间,后一个时间的处理等等

    自己做了一个例子:

    Java代码 复制代码
    1. package  com.observe.monitoralarm.util;  
    2.   
    3. import  java.text.ParseException;  
    4. import  java.text.SimpleDateFormat;  
    5. import  java.util.Calendar;  
    6. import  java.util.Date;  
    7.   
    8. /**  
    9.  * 时间帮助类  
    10.  * @version $Id: DateUtil.java,v 1.1 2008/05/28 04:29:52 linan Exp $  
    11.  * @author LiNan  
    12.  */   
    13. public   class  DateUtil {  
    14.   
    15.     private  Calendar calendar=Calendar.getInstance();  
    16.       
    17.     /**  
    18.      * 得到当前的时间,时间格式yyyy-MM-dd  
    19.      * @return  
    20.      */   
    21.     public  String getCurrentDate(){  
    22.         SimpleDateFormat sdf=new  SimpleDateFormat( "yyyy-MM-dd" );  
    23.         return  sdf.format( new  Date());  
    24.     }  
    25.       
    26.     /**  
    27.      * 得到当前的时间,自定义时间格式  
    28.      * y 年 M 月 d 日 H 时 m 分 s 秒  
    29.      * @param dateFormat 输出显示的时间格式  
    30.      * @return  
    31.      */   
    32.     public  String getCurrentDate(String dateFormat){  
    33.         SimpleDateFormat sdf=new  SimpleDateFormat(dateFormat);  
    34.         return  sdf.format( new  Date());  
    35.     }  
    36.       
    37.     /**  
    38.      * 日期格式化,默认日期格式yyyy-MM-dd  
    39.      * @param date  
    40.      * @return  
    41.      */   
    42.     public  String getFormatDate(Date date){  
    43.         SimpleDateFormat sdf=new  SimpleDateFormat( "yyyy-MM-dd" );  
    44.         return  sdf.format(date);  
    45.     }  
    46.       
    47.     /**  
    48.      * 日期格式化,自定义输出日期格式  
    49.      * @param date  
    50.      * @return  
    51.      */   
    52.     public  String getFormatDate(Date date,String dateFormat){  
    53.         SimpleDateFormat sdf=new  SimpleDateFormat(dateFormat);  
    54.         return  sdf.format(date);  
    55.     }  
    56.     /**  
    57.      * 返回当前日期的前一个时间日期,amount为正数 当前时间后的时间 为负数 当前时间前的时间  
    58.      * 默认日期格式yyyy-MM-dd  
    59.      * @param field 日历字段  
    60.      * y 年 M 月 d 日 H 时 m 分 s 秒  
    61.      * @param amount 数量  
    62.      * @return 一个日期  
    63.      */   
    64.     public  String getPreDate(String field, int  amount){  
    65.         calendar.setTime(new  Date());  
    66.         if (field!= null &&!field.equals( "" )){  
    67.             if (field.equals( "y" )){  
    68.                 calendar.add(calendar.YEAR, amount);  
    69.             }else   if (field.equals( "M" )){  
    70.                 calendar.add(calendar.MONTH, amount);  
    71.             }else   if (field.equals( "d" )){  
    72.                 calendar.add(calendar.DAY_OF_MONTH, amount);  
    73.             }else   if (field.equals( "H" )){  
    74.                 calendar.add(calendar.HOUR, amount);  
    75.             }  
    76.         }else {  
    77.             return   null ;  
    78.         }         
    79.         return  getFormatDate(calendar.getTime());  
    80.     }  
    81.       
    82.     /**  
    83.      * 某一个日期的前一个日期  
    84.      * @param d,某一个日期  
    85.      * @param field 日历字段  
    86.      * y 年 M 月 d 日 H 时 m 分 s 秒  
    87.      * @param amount 数量  
    88.      * @return 一个日期  
    89.      */   
    90.     public  String getPreDate(Date d,String field, int  amount){  
    91.         calendar.setTime(d);  
    92.         if (field!= null &&!field.equals( "" )){  
    93.             if (field.equals( "y" )){  
    94.                 calendar.add(calendar.YEAR, amount);  
    95.             }else   if (field.equals( "M" )){  
    96.                 calendar.add(calendar.MONTH, amount);  
    97.             }else   if (field.equals( "d" )){  
    98.                 calendar.add(calendar.DAY_OF_MONTH, amount);  
    99.             }else   if (field.equals( "H" )){  
    100.                 calendar.add(calendar.HOUR, amount);  
    101.             }  
    102.         }else {  
    103.             return   null ;  
    104.         }         
    105.         return  getFormatDate(calendar.getTime());  
    106.     }  
    107.       
    108.     /**  
    109.      * 某一个时间的前一个时间  
    110.      * @param date  
    111.      * @return  
    112.      * @throws ParseException   
    113.      */   
    114.     public  String getPreDate(String date)  throws  ParseException{  
    115.         Date d=new  SimpleDateFormat().parse(date);  
    116.         String preD=getPreDate(d,"d" , 1 );  
    117.         Date preDate=new  SimpleDateFormat().parse(preD);  
    118.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
    119.         return  sdf.format(preDate);  
    120.     }  
    121.       
    122.       

    //-----------------日期-------------------------

    Calendar calendar=Calendar.getInstance();
      int year=calendar.get(Calendar.YEAR);
      int month=calendar.get(Calendar.MONTH)+1;
      int day=calendar.get(Calendar.DATE);

    获取今天的日期字符串
    String today=java.text.DateFormat.getDateInstance().format(new java.util.Date());
    获取今天的日期
    new java.sql.Date(System.currentTimeMillis())

     DateFormatStyle.java

    package com.javaeye.lindows.util;
    
    import java.text.DateFormat;
    import java.util.Date;
    
    public class DateFormatStyle {
    	// 格式化时间: 09-4-8 下午3:57
    	public String formateTime() {
    		Date nowDate = new Date(); // 获取系统时间样式 Wed Apr 08 15:28:12 CST 2009
    		System.out.println(nowDate);
    		DateFormat dFormat = DateFormat.getInstance();
    		String string = dFormat.format(nowDate);
    		return string;
    	}
    
    	// 格式化时间1:下午04时00分36秒 CST
    	public String formateTime1() {
    		Date nowDate = new Date();
    		DateFormat dFormat1 = DateFormat.getTimeInstance(DateFormat.FULL);
    		String string1 = dFormat1.format(nowDate);
    		return string1;
    	}
    
    	// 格式化时间2:下午04时02分15秒
    	public String formateTime2() {
    		Date nowDate = new Date(); // 获取系统时间
    		DateFormat dFormat2 = DateFormat.getTimeInstance(DateFormat.LONG);
    		String string2 = dFormat2.format(nowDate);
    		return string2;
    	}
    
    	// 格式化时间3:下午4:05
    	public String formateTime3() {
    		Date nowDate = new Date(); // 获取系统时间
    		DateFormat dFormat3 = DateFormat.getTimeInstance(DateFormat.SHORT);
    		String string3 = dFormat3.format(nowDate);
    		return string3;
    	}
    
    	// 格式化时间4:2009年4月8日 星期三
    	public String formateTime4() {
    		Date nowDate = new Date(); // 获取系统时间
    		DateFormat dFormat4 = DateFormat.getDateInstance(DateFormat.FULL);
    		String string4 = dFormat4.format(nowDate);
    		return string4;
    	}
    
    	// 格式化时间5:2009年4月8日
    	public String formateTime5() {
    		Date nowDate = new Date(); // 获取系统时间
    		DateFormat dFormat5 = DateFormat.getDateInstance(DateFormat.LONG);
    		String string5 = dFormat5.format(nowDate);
    		return string5;
    	}
    
    	// 格式化时间6:09-4-8
    	public String formateTime6() {
    		Date nowDate = new Date(); // 获取系统时间
    		DateFormat dFormat6 = DateFormat.getDateInstance(DateFormat.SHORT);
    		String string6 = dFormat6.format(nowDate);
    		return string6;
    	}
    
    	public static void main(String[] args) {
    		DateFormatStyle testDate = new DateFormatStyle();
    		System.out.println("格式化时间: " + testDate.formateTime());
    		System.out.println("格式化时间1:" + testDate.formateTime1());
    		System.out.println("格式化时间2:" + testDate.formateTime2());
    		System.out.println("格式化时间3:" + testDate.formateTime3());
    		System.out.println("格式化时间4:" + testDate.formateTime4());
    		System.out.println("格式化时间5:" + testDate.formateTime5());
    		System.out.println("格式化时间6:" + testDate.formateTime6());
    	}
    }
    


    date.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ page import="com.javaeye.lindows.test.DateFormatStyle"%>
    <head></head>
    	<body>
    		<!-- 时间写法一 -->
    		<jsp:useBean id="DateFormatStyle_id"
    	class="com.javaeye.lindows.test.DateFormatStyle">当前时间是:</jsp:useBean>
    		<%--scriplet 注释--%>
    			<%=DateFormatStyle_id.formateTime2()%> 
    		<p>当前时间是:
    		<!-- 时间写法二 -->
    		<%
    			DateFormatStyle dateStyle = new DateFormatStyle();
    			out.print(dateStyle.formateTime2());
    		 %>
    	</body>
    </html>
    

    ------------------end--------------------

  • 相关阅读:
    k近邻 KNN
    聚类之k-means
    支持向量机SVM、优化问题、核函数
    [THUSC 2016] 补退选 (Trie树)
    [CQOI2016] 手机号码 (数位dp)
    [CQOI2012] 交换棋子 (费用流)
    [SCOI2016] 背单词 (Trie树)
    [JSOI2009] 球队收益 (费用流)
    [BZOJ1878][SDOI2009] HH的项链 (树状数组)
    [BZOJ2151]种树
  • 原文地址:https://www.cnblogs.com/lindows/p/14390584.html
Copyright © 2011-2022 走看看