日期操做

日期操做

java.util包中java

Dateapp

public class Dateextends Object implements Serializable, Cloneable, Comparable<Date>spa

此類是日期操做的一個重要的類。code

Calendar  抽象類 orm

public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar>對象

經過此類能夠將時間精確到毫秒,此類是一個抽象類,使用時必須依靠其子類。blog

GregorianCalendarci

public class GregorianCalendar extends Calendar 此類是Calendar的惟一子類。開發

日期的格式化

java.text包中字符串

Format

public abstract class Format extends Objectimplements Serializable, Cloneable

Format 是一個用於格式化語言環境敏感的信息(如日期、消息和數字)的抽象基類

實現它的子類有:DateFormat, MessageFormat, NumberFormat

DateFormat和SimpleDateFormat

public abstract class DateFormat extends Format

此類是個抽象類,開發中通常不直接使用,而是用SimpleDateFormat。

public class SimpleDateFormat extends DateFormat

經常使用的日期字符串轉換:

 1 package com.fwj.conver;
 2 
 3 import java.text.DateFormat;
 4 import java.text.ParseException;
 5 import java.text.SimpleDateFormat;
 6 import java.util.Date;
 7 
 8 public class Conver {
 9     public Conver() {
10         
11     }
12     
13     public static String ConverToString(Date date) {
14         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
15         String strDate = df.format(date);
16         return strDate;
17     }
18     
19     public static Date ConverToDate(String strDate) throws ParseException {
20         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
21         Date date = df.parse(strDate);
22         return date;
23     }
24 }

SimpleDateFormat例子:

 1 package com.fwj.dateformat;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 public class SimpleDateFormatDemo {
 8 
 9     public static void main(String[] args) throws ParseException {
10 
11         String str = "2012-6-12 09:45:52.632";
12         SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");//
13         SimpleDateFormat sf2 = new SimpleDateFormat("yyyy年MM月dd日hh時mm分ss秒SSS毫秒");
14         Date date = sf1.parse(str);//按照sf1給定的格式取得日期
15         String dateStr = sf2.format(date);//按照sf2給定的格式解析Date
16         System.out.println(dateStr);
17     }
18 }

運行結果:

2012年06月12日09時45分52秒632毫秒

使用SimpleDateFormat中的applyPattern()方法設定模式

 1 package com.fwj.dateformat;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 public class SimpleDateFormatDemo {
 8 
 9     public static void main(String[] args) throws ParseException {
10 
11         String str = "2012-6-12 09:45:52.632";
12         SimpleDateFormat sf = new SimpleDateFormat();
13         sf.applyPattern("yyyy-MM-dd hh:mm:ss.SSS");
14         Date date = sf.parse(str);//按照sf1給定的格式取得日期
15         sf.applyPattern("yyyy年MM月dd日hh時mm分ss秒SSS毫秒");
16         String dateStr = sf.format(date);//按照sf2給定的格式解析Date
17         System.out.println(dateStr);
18     }
19 }

NumberFormat和DecimalFormat

DecimalFormat是NumberFormat的子類

 1 package com.fwj.dateformat;
 2 
 3 import java.text.NumberFormat;
 4 
 5 public class NumberFormatDemo {
 6 
 7     public static void main(String[] args) {
 8 
 9         int num = 12032040;
10         NumberFormat nf = NumberFormat.getInstance();
11         System.out.println(nf.format(num));
12     }
13 
14 }

運行結果:

1 12,032,040

中文環境下每三位隔開。

下面看DecimalFormat例子:

DecimalFormat與SimpleDateFormate相似,在此類中也存在着一套模板設置。

 1 package com.fwj.dateformat;
 2 
 3 import java.text.DecimalFormat;
 4 
 5 public class FormatDemo {
 6 
 7     public void format(String pattern,double value){
 8         DecimalFormat df = new DecimalFormat(pattern);//指定操做模板
 9         String str = df.format(value);
10         System.out.println("使用"+pattern+"格式化數字"+value+"爲:"+str);
11     }
12 }
 1 package com.fwj.dateformat;
 2 
 3 public class FormatDemoTest {
 4 
 5     public static void main(String[] args) {
 6 
 7         FormatDemo demo = new FormatDemo();
 8         demo.format("###,###.###", 33652.36952);
 9         demo.format("000,000.000", 33652.36952);
10         demo.format("###,###.###¥", 33652.36952);
11         demo.format("000,000.000¥", 33652.36952);
12         demo.format("##.###%", 0.06952);
13         demo.format("00.###%", 0.06952);
14         demo.format("##.###\u2030", 0.06952);
15     }
16 }

運行結果:

使用###,###.###格式化數字33652.36952爲:33,652.37
使用000,000.000格式化數字33652.36952爲:033,652.370
使用###,###.###¥格式化數字33652.36952爲:33,652.37¥
使用000,000.000¥格式化數字33652.36952爲:033,652.370¥
使用##.###%格式化數字0.06952爲:6.952%
使用00.###%格式化數字0.06952爲:06.952%
使用##.###‰格式化數字0.06952爲:69.52‰

大數操做

大數操做是指數據很是大,大到已經超過了整個數據類型的保存範圍,應此,此時就須要使用對象的形式進行操做,若是碰到這種爲題,實際上都是使用字符串進行處理的。

爲了解決這一問題,Java中提供了兩大數字對象:BigInteger,BigDecimal

BigInteger

BigInteger表示大的整型數據

public class BigInteger extends Numberimplements Comparable<BigInteger>

 1 package com.fwj.dateformat;
 2 
 3 import java.math.BigInteger;
 4 
 5 public class BigIntegerDemo {
 6 
 7     public static void main(String[] args) {
 8 
 9         String mun = "88888888888888888888888888";
10         BigInteger bt1 = new BigInteger(mun);
11         BigInteger bt2 = new BigInteger(mun);
12         System.out.println("加法操做:"+bt1.add(bt2));
13     }
14 }

運行結果:

加法操做:177777777777777777777777776

BigDecimal

BigDecimal類的主要功能是進行小數的大數計算,最重要的是能夠精確到指定的四捨五入。

 1 package com.fwj.dateformat;
 2 
 3 import java.math.BigDecimal;
 4 
 5 public class BigIntegerDemo {
 6 
 7     public static void main(String[] args) {
 8 
 9         String mun = "88888888888888888888888888";
10         BigDecimal bt1 = new BigDecimal(mun);
11         BigDecimal bt2 = new BigDecimal(mun);
12         System.out.println("加法操做:"+bt1.add(bt2).doubleValue());
13     }
14 }

運行結果:

加法操做:1.7777777777777777E26
相關文章
相關標籤/搜索