验证中...
码云 Gitee IDE 全新上线——支持 Git 管理的轻量在线编码环境
语言: Java
分类: Web开发技术
最后更新于 2018-08-10 15:18
java日期工具类最佳实践
原始数据 复制代码
需要引入commons-lang包,Maven配置如下:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
importorg.apache.commons.lang3.time.FastDateFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Discribe:日期工具类
* Date: 2016/5/13
* Time: .19:05
*/
public class FastDateFormaTest {
/*patten*/
public final static String PATTEN_YEAR = "yyyy" ;
public final static String PATTEN_YEAR_MM = "yyyy-MM" ;
public final static String PATTEN_YEAR_MM_DD = "yyyy-MM-dd" ;
public final static String PATTEN_YEAR_MM_DD_HH = "yyyy-MM-dd HH" ;
public final static String PATTEN_YEAR_MM_DD_HH_MM = "yyyy-MM-dd HH:mm" ;
public final static String PATTEN_YEAR_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss" ;
public final static String PATTEN_YEAR_MM_DD_HH_MM_SS_MS = "yyyy-MM-dd HH:mm:ss:ms" ;
public static void main(String[] args) {
/**
* 测试10次
*/
/*for(int i=1;i<=10;i++){
testDateFormat(i);
}*/
Date date = new Date();
System.out.println(dateToString(date,PATTEN_YEAR));
System.out.println(dateToString(date,PATTEN_YEAR_MM));
System.out.println(dateToString(date,PATTEN_YEAR_MM_DD));
System.out.println(dateToString(date,PATTEN_YEAR_MM_DD_HH));
System.out.println(dateToString(date,PATTEN_YEAR_MM_DD_HH_MM));
System.out.println(dateToString(date,PATTEN_YEAR_MM_DD_HH_MM_SS));
System.out.println(dateToString(date,PATTEN_YEAR_MM_DD_HH_MM_SS_MS));
}
/**
* 指定日期及需输出格式,返回格式化后的字符串
* @param date
* @param patten
* @return
*/
public final static String dateToString(Date date,String patten) {
FastDateFormat fastDateFormat = FastDateFormat.getInstance(patten);
return fastDateFormat.format(date);
}
/**
* testDateFormat
*/
public static void testDateFormat(int num){
System.out.println("执行第"+num+"次");
int cont = 200000;
long l1 = System.currentTimeMillis();
for(int i=0;i<=cont;i++){
SimpleDateFormat s = new SimpleDateFormat(PATTEN_YEAR_MM_DD_HH_MM_SS);
Date date = new Date();
String string = s.format(date);
}
long l2 = System.currentTimeMillis();
System.out.println("SimpleDateFormat covert time:"+(l2-l1));
for(int i=0;i<=cont;i++){
FastDateFormat s = FastDateFormat.getInstance(PATTEN_YEAR_MM_DD_HH_MM_SS);
Date date = new Date();
String string = s.format(date);
//System.out.println(string);
}
long l3 = System.currentTimeMillis();
System.out.println("FastDateFormat covert time:"+(l3-l2));
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助