zoukankan      html  css  js  c++  java
  • Java:类 SimpleDateFormat的一个例子

    SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。

    SimpleDateFormat 使得可以选择任何用户定义的日期-时间格式的模式。但是,仍然建议通过 DateFormat 中的 getTimeInstancegetDateInstancegetDateTimeInstance 来创建日期-时间格式器。每一个这样的类方法都能够返回一个以默认格式模式初始化的日期/时间格式器。可以根据需要使用 applyPattern 方法来修改格式模式。

     

    实例:查看大数据量时冒泡排序执行的时间

    View Code
     1 // bubbleSort.java
     2 // demonstrates bubble sort
     3 // to run this program: C>java BubbleSortApp
     4 ////////////////////////////////////////////////////////////////
     5 class ArrayBub
     6    {
     7    private long[] a;                 // ref to array a
     8    private int nElems;               // number of data items
     9 //--------------------------------------------------------------
    10    public ArrayBub(int max)          // constructor
    11       {
    12       a = new long[max];                 // create the array
    13       nElems = 0;                        // no items yet
    14       }
    15 //--------------------------------------------------------------
    16    public void insert(long value)    // put element into array
    17       {
    18       a[nElems] = value;             // insert it
    19       nElems++;                      // increment size
    20       }
    21 //--------------------------------------------------------------
    22    public void display()             // displays array contents
    23       {
    24       for(int j=0; j<nElems; j++)       // for each element,
    25          System.out.print(a[j] + " ");  // display it
    26       System.out.println("");
    27       }
    28 //--------------------------------------------------------------
    29    public void bubbleSort()
    30       {
    31       int out, in;
    32 
    33       for(out=nElems-1; out>1; out--)   // outer loop (backward)
    34          for(in=0; in<out; in++)        // inner loop (forward)
    35             if( a[in] > a[in+1] )       // out of order?
    36                swap(in, in+1);          // swap them
    37       }  // end bubbleSort()
    38 //--------------------------------------------------------------
    39    private void swap(int one, int two)
    40       {
    41       long temp = a[one];
    42       a[one] = a[two];
    43       a[two] = temp;
    44       }
    45 //--------------------------------------------------------------
    46    }  // end class ArrayBub
    View Code
     1 ////////////////////////////////////////////////////////////////
     2 import java.util.Date;
     3 import java.text.SimpleDateFormat;
     4 class BubbleSortApp
     5    {
     6    public static void main(String[] args)
     7       {
     8   int maxSize = 20000;            // array size
     9       ArrayBub arr;                 // reference to array
    10       arr = new ArrayBub(maxSize);  // create the array
    11       
    12       for(int j=0; j<maxSize; j++) // fill array with random numbers
    13       { 
    14           long n = (long)( java.lang.Math.random()*(maxSize-1) );
    15           arr.insert(n);
    16         }    
    17 
    18         System.out.println("before sorted");
    19         //arr.display();                // display items
    20         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    21         Date before = new Date();
    22         System.out.println(df.format(before));// new Date()
    23 
    24         System.out.println("Sorting...");// new Date() 
    25         arr.bubbleSort();             // bubble sort them
    26     
    27         System.out.println("after sorted");
    28         Date after = new Date();    
    29         System.out.println(df.format(after));    
    30         long interval=after.getTime()-before.getTime();
    31 
    32         System.out.println(interval);
    33 //    arr.display();                // display them again
    34       }  // end main()
    35    }  // end class BubbleSortApp
    36 ////////////////////////////////////////////////////////////////

     

    执行结果:

     

  • 相关阅读:
    JSON 数据转换
    .NET LINQ 数据排序
    .NET LINQ标准查询运算符
    UML 序列图
    UML 类图
    UML 用例建模
    UML 概述
    .NET LINQ查询语法与方法语法
    .NET LINQ查询操作中的类型关系
    .NET 反射概述
  • 原文地址:https://www.cnblogs.com/KeenLeung/p/2744056.html
Copyright © 2011-2022 走看看