﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gavin&#039;s Blog &#187; 时间段</title>
	<atom:link href="http://laigw.name/tag/%e6%97%b6%e9%97%b4%e6%ae%b5/feed" rel="self" type="application/rss+xml" />
	<link>http://laigw.name</link>
	<description>Keep it simple, stupid. Simplicity is beauty.</description>
	<lastBuildDate>Sun, 29 Jan 2012 07:14:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>学习在java中计算基本的时间段</title>
		<link>http://laigw.name/post/19.html</link>
		<comments>http://laigw.name/post/19.html#comments</comments>
		<pubDate>Fri, 14 Nov 2008 12:18:25 +0000</pubDate>
		<dc:creator>Gavin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[时间]]></category>
		<category><![CDATA[时间段]]></category>

		<guid isPermaLink="false">http://www.laigw.name/?p=19</guid>
		<description><![CDATA[学习在java中计算基本的时间段
概述
如果你知道怎样在java中使用日期，那么使用时间和它才不多一样简单。这篇文章告诉你怎样把他们的差别联系起来。Robert Nielsen还告诉你怎样使用java来计算抵达航班和制造过程的时间。
作者：Robert Nielsen
翻译：Cocia Lin
这篇文章是在我发表过的&#60;计算Java时间&#62;(译者:已经翻译完成)的基础上的。在这里，我列出那篇文章几个你应该熟悉得关键点。如果这几点你不太清楚，我建议你读一下&#60;计算Java时间&#62;，了解一下。
1. Java计算时间依靠1970年1月1日开始的毫秒数.
2. Date类的构造函数Date()返回代表当前创建的时刻的对象。Date的方法getTime()返回一个long值在数值上等于1970年1月1日之前或之后的时刻。
3. DateFormat类用来转换Date到String,反之亦然。静态方法getDateInstance()返回DateFormat的缺省格式；getDateInstance(DateFormat.FIELD)返回指定的DateFormat对象格式。Format(Date d)方法返回String表示日期，例如＂January 1,2002.＂反过来，parse(String s)方法返回以参数字符串表示的Date对象。
4. format()方法返回的字符串格式根据不同地区的时间设置而有所不同。
5. GregorianCalendear类有两个重要的构造函数：GregorianCalerdar(),返回代表当前创建时间的对象；GregorianCalendar(int year,int month,int date)返回代表任意日期的对象。GregorianCalendar类的getTime()方法返回日期对象。Add(int field,int amount)方法通过加或减时间单位，象天数，月数或年数来计算日期。
GregorianCalendar和 时间
两个GregorianCalendar的构造函数可以用来处理时间。前者创建一个表示日期，小时和分钟的对象：
GregorianCalendar(int year, int month, int date, int hour, int minute)
第二个创建一个表示一个日期，小时，分钟和秒：
GregorianCalendar(int year, int month, int date, int hour, int minute, int second)

首先，我应该提醒一下，每一个构造函数需要时间信息中的日期信息(年，月，日)。如果你想说2:30 p.m.,你必须指出日期。
同样，每一个GregorianCalendar构造函数创建一个在时间上使用毫秒计算的对象。所以，如果你的构造函数只提供年，月，日参数，那小时，分钟，秒和毫秒的值将被置0.
DateFormat和时间
你可以使用静态方法getDateTimeInstance(int dateStyle,int timeStyle)来建立DateFormat对象来显示时间和日期。这个方法表明你想要的日期和时间格式。如果你喜欢使用缺省格式，可以使用getDateTimeInstance()来代替它。
你可以使用静态方法getTimeInstance(int timeStyle)创建DateFormat对象来显示正确的时间。
下面的程序示范了getDateTimeInstance()和getTimeInstance()怎样工作：

&#160;查看代码 JAVA1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;
import java.text.*;
&#160;
public class Apollo &#123;
public static void main&#40;String&#91;&#93; args&#41; &#123;
GregorianCalendar liftOffApollo11 = [...]]]></description>
		<wfw:commentRss>http://laigw.name/post/19.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

