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
- 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());
- }
- }
- }
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());
}
}
}
时间无非就是字符串类型转向时间类型,或则时间类型转向字符串类型,还有就是前一个时间,后一个时间的处理等等
自己做了一个例子:
- package com.observe.monitoralarm.util;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * 时间帮助类
- * @version $Id: DateUtil.java,v 1.1 2008/05/28 04:29:52 linan Exp $
- * @author LiNan
- */
- public class DateUtil {
- private Calendar calendar=Calendar.getInstance();
- /**
- * 得到当前的时间,时间格式yyyy-MM-dd
- * @return
- */
- public String getCurrentDate(){
- SimpleDateFormat sdf=new SimpleDateFormat( "yyyy-MM-dd" );
- return sdf.format( new Date());
- }
- /**
- * 得到当前的时间,自定义时间格式
- * y 年 M 月 d 日 H 时 m 分 s 秒
- * @param dateFormat 输出显示的时间格式
- * @return
- */
- public String getCurrentDate(String dateFormat){
- SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
- return sdf.format( new Date());
- }
- /**
- * 日期格式化,默认日期格式yyyy-MM-dd
- * @param date
- * @return
- */
- public String getFormatDate(Date date){
- SimpleDateFormat sdf=new SimpleDateFormat( "yyyy-MM-dd" );
- return sdf.format(date);
- }
- /**
- * 日期格式化,自定义输出日期格式
- * @param date
- * @return
- */
- public String getFormatDate(Date date,String dateFormat){
- SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
- return sdf.format(date);
- }
- /**
- * 返回当前日期的前一个时间日期,amount为正数 当前时间后的时间 为负数 当前时间前的时间
- * 默认日期格式yyyy-MM-dd
- * @param field 日历字段
- * y 年 M 月 d 日 H 时 m 分 s 秒
- * @param amount 数量
- * @return 一个日期
- */
- public String getPreDate(String field, int amount){
- calendar.setTime(new Date());
- if (field!= null &&!field.equals( "" )){
- if (field.equals( "y" )){
- calendar.add(calendar.YEAR, amount);
- }else if (field.equals( "M" )){
- calendar.add(calendar.MONTH, amount);
- }else if (field.equals( "d" )){
- calendar.add(calendar.DAY_OF_MONTH, amount);
- }else if (field.equals( "H" )){
- calendar.add(calendar.HOUR, amount);
- }
- }else {
- return null ;
- }
- return getFormatDate(calendar.getTime());
- }
- /**
- * 某一个日期的前一个日期
- * @param d,某一个日期
- * @param field 日历字段
- * y 年 M 月 d 日 H 时 m 分 s 秒
- * @param amount 数量
- * @return 一个日期
- */
- public String getPreDate(Date d,String field, int amount){
- calendar.setTime(d);
- if (field!= null &&!field.equals( "" )){
- if (field.equals( "y" )){
- calendar.add(calendar.YEAR, amount);
- }else if (field.equals( "M" )){
- calendar.add(calendar.MONTH, amount);
- }else if (field.equals( "d" )){
- calendar.add(calendar.DAY_OF_MONTH, amount);
- }else if (field.equals( "H" )){
- calendar.add(calendar.HOUR, amount);
- }
- }else {
- return null ;
- }
- return getFormatDate(calendar.getTime());
- }
- /**
- * 某一个时间的前一个时间
- * @param date
- * @return
- * @throws ParseException
- */
- public String getPreDate(String date) throws ParseException{
- Date d=new SimpleDateFormat().parse(date);
- String preD=getPreDate(d,"d" , 1 );
- Date preDate=new SimpleDateFormat().parse(preD);
- SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
- return sdf.format(preDate);
- }
- }
//-----------------日期-------------------------
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--------------------