`

java timer / date

    博客分类:
  • Java
阅读更多

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--------------------

 

 

分享到:
评论

相关推荐

    android date & timer study

    android time and date java code

    java倒计时源代码 原创

    public static void main(String[] args) { ... timer.schedule(new MyTask(date),0,1000); } //ps:如有不懂请联系作者!解压后是两个java类,自己创建java项目,把两个类拷贝进去,修改以下包路径即可

    java定时器

    java定时器,import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; 规定每天几点执行一次

    Java2实用教程.rar

    9 9计时器线程Timer 9 10线程联合 9 11守护线程 习题 第10章输入输出流 10 1File类 10 2FileInputStream类 10 3FileOutputStream类 10 4FileReader类和FileWriter类 10 5使用文件对话框打开和保存文件 10 6...

    Java之Timer类的使用以及深入理解

    近一直在看线程知识,然后看到Timer定时器使用了线程实现的定时功能,于是了解了解;  本文 从Time类的使用和源码分析两个方面讲解:  1、Time类使用: 1 根据是否循环执行分为两类: 2 3 //只执行一次 4...

    自己用java写的倒计时代码

    //get the date of today Calendar newyears = new GregorianCalendar(2008,Calendar.AUGUST,8); //2008.8.8 long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();//Get difference in ms long...

    java时间控制代码

    通过自定义时间段 控制线程的运行时间 i.Date; .Timer;

    java范例开发大全源代码

    第1篇 Java编程基础  第1章 Java开发环境的搭建(教学视频:9分钟) 2  1.1 理解Java 2  1.2 搭建Java所需环境 3  1.2.1 下载JDK 3  1.2.2 安装JDK 4  1.2.3 配置环境 5  1.2.4 测试JDK配置...

    java范例开发大全

    实例240 定时器(Timer) 458 实例241 数字定时器 459 13.5 线程连接池 462 实例242 手术任务(线程池) 462 实例243 模拟人工服务台(线程连接池) 466 13.6 线程应用实例 471 实例244 下雪的村庄 472 实例245 小...

    Java范例开发大全 (源程序)

    第1篇 Java编程基础  第1章 Java开发环境的搭建(教学视频:9分钟) 2  1.1 理解Java 2  1.2 搭建Java所需环境 3  1.2.1 下载JDK 3  1.2.2 安装JDK 4  1.2.3 配置环境 5  1.2.4 测试JDK配置是否成功 7...

    Android 实现定时器的四种方式总结及实现实例

    java.util.Timer 基本方法 schedule 例如: timer.schedule(task, delay,period); //delay为long,period为long:从现在起过delay毫秒以后,每隔period毫秒执行一次。 schedule方法有三个参数 第一个参数就是...

    Java范例开发大全(全书源程序)

    实例240 定时器(Timer) 458 实例241 数字定时器 459 13.5 线程连接池 462 实例242 手术任务(线程池) 462 实例243 模拟人工服务台(线程连接池) 466 13.6 线程应用实例 471 实例244 下雪的村庄 472 实例...

    java范例开发大全(pdf&源码)

    实例240 定时器(Timer) 458 实例241 数字定时器 459 13.5 线程连接池 462 实例242 手术任务(线程池) 462 实例243 模拟人工服务台(线程连接池) 466 13.6 线程应用实例 471 实例244 下雪的村庄 472 实例245 小...

    合并排序算法的演示

    import java.util.Date; import java.util.GregorianCalendar; import java.util.Timer; import java.util.TimerTask; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import...

    D2-Arbito-Herrera-Velastegui-8D_java_net_netbeans_ThroughtheWind

    Duty 2 Schedule 3======================In chapter 7 of the book section Implementing aspect oriented programming with interceptorson page 255 through page 259 just before the EJB timer service section...

    java的定时器使用方法.txt

    定时器类Timer在java.util包中。使用时,先实例化,然后使用实例的schedule(TimerTask task, long delay)方法,设定指定的任务task在指定的延迟delay后执行。定时器任务类TimerTask是抽象类,继承并重写其run()方法...

    秒表计时器设计,java程序设计

    计时器计时功能的控制用到了Date类和Timer类,为了让所做的图形界面程序通过事件响应机制实现用户与程序的交互,通过在按钮单击事件里添加一个监控对象,然后使用接口对象的方法来完成相应事件的处理。本程序中还...

    Java学习笔记-个人整理的

    {13.8}java.util.Date与java.sql.Date比较}{200}{section.13.8} {13.9}Meta Data}{201}{section.13.9} {13.10}可滚动结果集}{201}{section.13.10} {13.11}Procedure}{201}{section.13.11} {14}xml}{204}{...

    freemarker总结

    JAVA模版引擎Freemarker常用标签(一) 1. if指令 这是一个典型的分支控制指令,该指令的作用完全类似于Java语言中的if,if指令的语法格式如下: &lt;#if condition&gt;... &lt;#elseif condition&gt;... &lt;#elseif condition&gt;......

    TaskSystem4j:一个易于使用计时器创建基于Java的任务的库

    import java.util.Date ; import java.util.concurrent.TimeUnit ; import com.laurencetrippen.tasksystem4j.Task ; import com.laurencetrippen.tasksystem4j.TaskSystem ; public class Tester { private static...

Global site tag (gtag.js) - Google Analytics