本篇用代碼示例結(jié)合JDk源碼及本人的理解講了Java8新的時(shí)間API以及Repeatable Annotation.
參考:http://winterbe.com/posts/2014/03/16/java-8-tutorial/
1.java.time API
package com.mavsplus.java8.turtorial.time;

import java.time.Clock;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
import java.util.Set;


/** *//**
* Java 8 包含了全新的時(shí)間日期API,這些功能都放在了java.time包下。新的時(shí)間日期API是基于Joda-Time庫開發(fā)的,但是也不盡相同
*
* <p>
* public abstract class Clock {
*
* @author landon
* @since 1.8.0_25
*/

public class TimeAPIExample
{


/** *//**
* Clock提供了對當(dāng)前時(shí)間和日期的訪問功能。Clock是對當(dāng)前時(shí)區(qū)敏感的,并可用于替代System.currentTimeMillis()
* 方法來獲取當(dāng)前的毫秒時(shí)間。當(dāng)前時(shí)間線上的時(shí)刻可以用Instance類來表示。Instance也能夠用于創(chuàng)建原先的java.util.Date對象。
*/

public void useClock()
{
Clock clock = Clock.systemDefaultZone();

long clockMillis = clock.millis();
long curMillis = System.currentTimeMillis();

// %d格式包括了int/long等
// 從輸出可以看出,二者完全相同
System.out.format("clockMillis:%d,curMillis:%d", clockMillis, curMillis).println();

Instant instant = clock.instant();
// 這里lagacy可以理解為傳統(tǒng)的
Date legacyDate = Date.from(instant);

System.out.println(String.format("legacyDate:%d", legacyDate.getTime()));
}


/** *//**
* 時(shí)區(qū)類可以用一個(gè)ZoneId來表示。時(shí)區(qū)類的對象可以通過靜態(tài)工廠方法方便地獲取。時(shí)區(qū)類還定義了一個(gè)偏移量,
* 用來在當(dāng)前時(shí)刻或某時(shí)間與目標(biāo)時(shí)區(qū)時(shí)間之間進(jìn)行轉(zhuǎn)換
*/

public void useTimeZone()
{
// public abstract class ZoneId implements Serializable

// 迭代可用的時(shí)區(qū)
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
// 現(xiàn)學(xué)現(xiàn)賣!lambda!
availableZoneIds.stream().forEach(System.out::println);

ZoneId zoneId1 = ZoneId.of("Asia/Shanghai");
ZoneId zoneId2 = ZoneId.of("America/New_York");

// 輸出:ZoneRules[currentStandardOffset=+08:00]
System.out.println(zoneId1.getRules());
// 輸出:ZoneRules[currentStandardOffset=-05:00]
System.out.println(zoneId2.getRules());
}


/** *//**
* 本地時(shí)間類表示一個(gè)沒有指定時(shí)區(qū)的時(shí)間,例如,10
* p.m.或者17:30:15,下面的例子會(huì)用上面的例子定義的時(shí)區(qū)創(chuàng)建兩個(gè)本地時(shí)間對象。然后我們會(huì)比較兩個(gè)時(shí)間,并計(jì)算它們之間的小時(shí)和分鐘的不同
*
* LocalTime是由多個(gè)工廠方法組成,其目的是為了簡化對時(shí)間對象實(shí)例的創(chuàng)建和操作,包括對時(shí)間字符串進(jìn)行解析的操作
*/

public void useLocalTime()
{
ZoneId zoneId1 = ZoneId.of("Asia/Shanghai");
ZoneId zoneId2 = ZoneId.of("America/New_York");

LocalTime now1 = LocalTime.now(zoneId1);
LocalTime now2 = LocalTime.now(zoneId2);

System.out.println(now1.isBefore(now2));
System.out.println(now1.isAfter(now2));

// public enum ChronoUnit implements TemporalUnit
// Chrono_計(jì)時(shí)器
long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);

// 因?yàn)橐粋€(gè)是s+8,一個(gè)是s-5,相差13個(gè)小時(shí)
System.out.println(hoursBetween);
System.out.println(minutesBetween);

LocalTime lt = LocalTime.of(16, 19, 15);
System.out.println(lt);

// public final class DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(
Locale.SIMPLIFIED_CHINESE);
// 注意這里必須要用"下午6:30"(或者上午),因?yàn)長ocale選擇的是中國,否則其他形式報(bào)錯(cuò)
LocalTime lt2 = LocalTime.parse("下午6:30", formatter);
// 輸出:18:30
System.out.println(lt2);
}


/** *//**
* 本地日期表示了一個(gè)獨(dú)一無二的時(shí)間,例如:2014-03-11。這個(gè)時(shí)間是不可變的,與LocalTime是同源的。下面的例子演示了如何通過加減日,月
* , 年等指標(biāo)來計(jì)算新的日期。記住,每一次操作都會(huì)返回一個(gè)新的時(shí)間對象
*
* 解析字符串并形成LocalDate對象,這個(gè)操作和解析LocalTime一樣簡單
*/

public void useLocalDate()
{
LocalDate today = LocalDate.now();
// 輸出:2014-11-19
System.out.println(today);

// 獲取明天
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
// 獲取昨天
LocalDate yesterday = tomorrow.minusDays(2);

System.out.println(String.format("tomorrow:%s,yesterday:%s", tomorrow, yesterday));

// 國慶節(jié)
LocalDate nationalDay = LocalDate.of(2014, Month.OCTOBER, 1);
DayOfWeek dayOfWeek = nationalDay.getDayOfWeek();

// 2014年10月1是星期三
// 輸出:WEDNESDAY
System.out.println(dayOfWeek);

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(
Locale.SIMPLIFIED_CHINESE);
// 注意這里第一個(gè)參數(shù)文本"2014-11-19",目前測試必須這樣,因?yàn)長ocale為中國
LocalDate ld = LocalDate.parse("2014-11-19", formatter);

System.out.println(ld);
}


/** *//**
* LocalDateTime表示的是日期-時(shí)間。它將剛才介紹的日期對象和時(shí)間對象結(jié)合起來,形成了一個(gè)對象實(shí)例。LocalDateTime是不可變的,
* 與LocalTime和LocalDate的工作原理相同。我們可以通過調(diào)用方法來獲取日期時(shí)間對象中特定的數(shù)據(jù)域
*/

public void useLocalDateTime()
{
LocalDateTime end2014 = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);

// 2014年12月31日是星期三
DayOfWeek dayOfWeek = end2014.getDayOfWeek();
// 輸出:WEDNESDAY
System.out.println(dayOfWeek);

Month month = end2014.getMonth();
// 輸出:DECEMBER
System.out.println(month);

// This counts the minute within the day(23 * 60 + 59 = 1439)
long minuteOfDay = end2014.getLong(ChronoField.MINUTE_OF_DAY);
// 輸出:1439
System.out.println(minuteOfDay);

// 如果再加上的時(shí)區(qū)信息,LocalDateTime能夠被轉(zhuǎn)換成Instance實(shí)例。Instance能夠被轉(zhuǎn)換成以前的java.util.Date對象
Instant instant = end2014.atZone(ZoneId.systemDefault()).toInstant();
Date legacyDate = Date.from(instant);

// 輸出:Wed Dec 31 23:59:59 CST 2014
System.out.println(legacyDate);

// 格式化日期-時(shí)間對象就和格式化日期對象或者時(shí)間對象一樣。除了使用預(yù)定義的格式以外,我們還可以創(chuàng)建自定義的格式化對象,然后匹配我們自定義的格式。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy,MM-dd HH:mm");
LocalDateTime parsed = LocalDateTime.parse("2014,11-19 19:23", formatter);

System.out.println(formatter.format(parsed));

// 不同于java.text.NumberFormat,新的DateTimeFormatter類是不可變的,也是線程安全的
// ->因?yàn)閜ublic final class DateTimeFormatter->final的
}


public static void main(String[] args)
{
TimeAPIExample example = new TimeAPIExample();

example.useClock();
example.useTimeZone();
example.useLocalTime();
example.useLocalDate();
example.useLocalDateTime();
}
}

2.Repeatable Annotation
package com.mavsplus.java8.turtorial.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/** *//**
* Annotations in Java 8 are repeatable
*
* @author landon
* @since 1.8.0_25
*/

public class AnnotationRepeatableExample
{

// 包裝注解,其包括了Hint的數(shù)組
// 這里必須加上@Retention(RetentionPolicy.RUNTIME),因?yàn)檫\(yùn)行時(shí)反射要讀取該信息
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented

public @interface Hints
{
Hint[] value();
}


/** *//**
* <code>
*
* @Documented
* @Retention(RetentionPolicy.RUNTIME)
* @Target(ElementType.ANNOTATION_TYPE) public @interface Repeatable {
* Class<? extends Annotation> value();
* } </code>
*
*/

// 實(shí)際的注解
// 這里必須加上@Retention(RetentionPolicy.RUNTIME),因?yàn)檫\(yùn)行時(shí)反射要讀取該信息
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Hints.class)

public @interface Hint
{
String str();
}

// 老辦法

@Hints(
{ @Hint(str = "hint1"), @Hint(str = "hint2") })

public class Bean
{
}

// 新方法,使用可重復(fù)注解

// 如果Hint注解沒有加上Repetable則會(huì)編譯錯(cuò)誤
// Only annotation types marked @Repeatable can be used multiple times at
// one target.
@Hint(str = "hint1")
@Hint(str = "hint2")

public class Bean2
{
}

// 使用變體2,Java編譯器能夠在內(nèi)部自動(dòng)對@Hint進(jìn)行設(shè)置

public void reflectAnnotation() throws Exception
{
Hint hint = Bean2.class.getAnnotation(Hint.class);
// 輸出null
System.out.println(hint);

// 注意這里是$Bean
Hints hints3 = Class.forName("com.mavsplus.java8.turtorial.annotation.AnnotationRepeatableExample$Bean")
.getAnnotation(Hints.class);
// 輸出2
System.out.println(hints3.value().length);

Hints hints4 = Bean.class.getAnnotation(Hints.class);
// 輸出2
System.out.println(hints4.value().length);

// 這里傳入Hints.class依然可以獲取注解信息(因?yàn)镠int上有@Repeatable(Hints.class)這個(gè)注解)
// 盡管我們沒有在Bean2類上聲明@Hints注解,但是它的信息仍然可以通過getAnnotation(Hints.class)來讀取
Hints hints1 = Bean2.class.getAnnotation(Hints.class);
// 輸出2
System.out.println(hints1.value().length);

// getAnnotationsByType方法會(huì)更方便,因?yàn)樗x予了所有@Hint注解標(biāo)注的方法直接的訪問權(quán)限。
// 看一下這段注釋:
// The difference between this method and getAnnotation(Class)
// is that this method detects if its argument is a repeatable
// annotation type (JLS 9.6), and if so, attempts to find one or more
// annotations of that type by "looking through" a container annotation
Hint[] hints2 = Bean2.class.getAnnotationsByType(Hint.class);
// 輸出2
System.out.println(hints2.length);
}


public static void main(String[] args) throws Exception
{
AnnotationRepeatableExample example = new AnnotationRepeatableExample();

example.reflectAnnotation();
}
}

package com.mavsplus.java8.turtorial.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/** *//**
* 和AnnotationRepeatableExample對比的一個(gè)例子
*
* @author landon
* @since 1.8.0_25
*/

public class AnnotationExample
{

// 經(jīng)測試發(fā)現(xiàn),這段代碼要通過編譯, WrappedAnno中的方法名必須為value.
// 同時(shí)參考了@Target源代碼以及@Retention源代碼,發(fā)現(xiàn)方法命名也為value.

// 解釋(by landon)
// 1.個(gè)人認(rèn)為注解內(nèi)部方法返回值如果為數(shù)組的話,如果方法名為value的話,可以不用寫類似于Anno(xxx =
// {}),可直接寫Anno({}),參見AnnoModified2
// 2.其實(shí)不一定返回值為數(shù)組,只要方法名定義為value的話,則賦值的話均無須指明value=xxx,參見AnnoModified3

// 總結(jié):編譯器默認(rèn)注解內(nèi)部方法名為value. 注:當(dāng)且僅當(dāng)注解內(nèi)部只有一個(gè)方法時(shí)可以這樣,參見AnnoModified4

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented

@interface WrappedAnno
{
Anno[] value();
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented

@interface Anno
{
String name();

int num();
}


@WrappedAnno(
{ @Anno(name = "value1", num = 1), @Anno(name = "value2", num = 2) })

class AnnoModified
{
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented

@interface WrappedAnno2
{
String[] values();
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented

@interface WrappedAnno3
{
int value();
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented

@interface WrappedAnno4
{
int value();

String name();
}


@WrappedAnno2(values =
{ "HI", "Anno" })

class AnnoModified2
{
}

@WrappedAnno3(2)

class AnnoModified3
{
}

@WrappedAnno4(value = 4, name = "Anno")

class AnnoModified4
{
}
}

posted on 2014-11-20 14:37
landon 閱讀(5628)
評論(0) 編輯 收藏 所屬分類:
Program