2016-03-29 4 views
5

के साथ ग्रोवी का टाइम कैटलियन जावा 8 लोकलडेट और लोकलडेटाइम के साथ टाइमकैरिटी (या समकक्ष) का उपयोग करने के तरीके या उदाहरण हैं? मुझे मिले सभी कोड स्निपेट java.util.Date को संदर्भित करते हैं जिन्हें मैं टालने का प्रयास कर रहा हूं।स्थानीय डेटा और स्थानीयडेटाइम

उत्तर

3
साथ जावा 8 LOCALDATE हेरफेर के रूप में सरल किया जा रहा

:

LocalDate.now().plusDays(2) 

मुझे यकीन है कि TimeCategory आप क्या हासिल होगा नहीं कर रहा हूँ?


आप LOCALDATE और LocalDatTime काफी बस की metaClass करने के लिए यह हैक कर सकते हैं:

import groovy.time.* 
import java.time.* 

LocalDate.metaClass { 
    plus { Duration d -> 
     delegate.plusYears(d.years) 
       .plusMonths(d.months) 
       .plusDays(d.days) 
    } 
} 

LocalDateTime.metaClass { 
    plus { Duration d -> 
     delegate.plusYears(d.years) 
       .plusMonths(d.months) 
       .plusDays(d.days) 
       .plusHours(d.hours) 
       .plusMinutes(d.minutes) 
       .plusSeconds(d.seconds) 
    } 
} 

use(TimeCategory) { 
    LocalDateTime.now() + 4.days 
} 
+0

ऊपर मेरी टिप्पणी देखेंगे। यह पठनीयता और मेरे लक्षित दर्शकों के बारे में है जो जावा में बहुत धाराप्रवाह नहीं हैं। – Pierre

+1

@Pierre आप बिना किसी परेशानी के एलडी और एलडीटी को समर्थन जोड़ सकते हैं ... देखें –

+0

बिल्कुल सही! अभी भी groovy गतिशील प्रकृति के लिए उपयोग नहीं किया जाता है और यह बाहरी और यहां तक ​​कि अंतिम कक्षाओं में कस्टम समर्थन जोड़ने का एक शानदार उदाहरण है! – Pierre

2

एक तुच्छ उदाहरण होगा:

import groovy.time.TimeCategory 
import java.time.LocalDate 
import java.time.LocalDateTime 

use(TimeCategory) { 
    println Date.parse('yyyy-MM-dd', LocalDate.now().toString()) + 4.hours 
    println Date.parse("yyyy-MM-dd'T'hh:mm:ss", LocalDateTime.now().toString()) - 4.hours 
} 
+0

वहाँ java8 समय संस्थाओं के लिए कोई देशी समर्थन है

आगे की हलचल के बिना

? आपके द्वारा सूचीबद्ध उदाहरण हालांकि वैध है वर्बोज़ का तरीका है और शाब्दिक रूप से TimeCategory का उपयोग करने के बिंदु को मारता है। – Pierre

+1

नहीं, इसमें कोई नहीं है। मैं मानता हूं कि यह वर्बोज़ है। यदि आप समय कुशलता की तलाश में हैं तो लोकलडेट टाइम और लोकलडेट एपीआई में पहले से ही 'plusHours() 'जैसी विधियां हैं। क्या आप ऐसा कुछ ढूंढ रहे हैं? – dmahapatro

+0

हां। जावा मेरे अंदर मजबूत है :) और सभी स्थानीय तारीख और समय में हेरफेर कार्यों के बारे में पूरी तरह से अवगत है। हालांकि एक निश्चित पठनीयता है कि टाइमकैबिन प्रदान करता है कि मेरे लक्षित दर्शकों (क्यूए) को अपील करता है जो कि बेहतर है। मुझे लगता है कि मैं बस java8 समय सीधे – Pierre

2

मैं भी निराश थे TimeCategory घोषणा करता है कि यह खुद अवधि है और जावा 8 अनुकूल नहीं है। इससे मुझे अपना खुद का कोड लिखने के लिए प्रेरित किया गया, जिसे मैंने सोचा था कि इस सवाल के लिए प्रासंगिक होगा। LocalDateTime के विपरीत यह ZonedDateTime पर केंद्रित है क्योंकि मुझे टाइमज़ोन तर्क में दिलचस्पी है जहां मैं इसका उपयोग कर रहा हूं। यह groovy.time.TimeCategory वर्ग के रूप में लगभग पूरा नहीं है, और इसमें केवल कुछ हद तक ऑपरेशन हैं जिनकी मुझे रूचि थी, इसलिए इसमें शामिल होने के लिए स्वतंत्र महसूस करें।

import java.time.Duration 
import java.time.ZoneOffset 
import java.time.ZonedDateTime 
import java.time.format.DateTimeFormatter 
import java.time.temporal.ChronoUnit 

class TimeCategory { 
    static getDays(Integer self) { Duration.ofDays self } 
    static getHours(Integer self) { Duration.ofHours self } 
    static getMillis(Integer self) { Duration.ofMillis self } 
    static getMinutes(Integer self) { Duration.ofMinutes self } 
    static getNanos(Integer self) { Duration.ofNanos self } 
    static getSeconds(Integer self) { Duration.ofSeconds self } 
    static getWeeks(Integer self) { Duration.ofDays self * 7 } 
    static getDay(Integer self) { Duration.ofDays self } 
    static getHour(Integer self) { Duration.ofHours self } 
    static getMilli(Integer self) { Duration.ofMillis self } 
    static getMinute(Integer self) { Duration.ofMinutes self } 
    static getNano(Integer self) { Duration.ofNanos self } 
    static getSecond(Integer self) { Duration.ofSeconds self } 
    static getWeek(Integer self) { Duration.ofDays self * 7 } 
    static ZonedDateTime getAgo(Duration self) { ZonedDateTime.now() - self } 
    static ZonedDateTime getUtc(ZonedDateTime self) { self.withZoneSameInstant(ZoneOffset.UTC) } 
    static ZonedDateTime getLocal(ZonedDateTime self) { self.withZoneSameInstant(ZoneOffset.systemDefault()) } 
    static ZonedDateTime getNow() { ZonedDateTime.now() } 
    static Duration minus(ZonedDateTime self, ZonedDateTime other) { Duration.between(self, other) } 
    static BigInteger getTotalNanos(Duration self) { self.seconds.toBigInteger() * 10 ** 9 + self.nano } 
    static String toString(ZonedDateTime self, String pattern) { self.format(DateTimeFormatter.ofPattern(pattern)) } 
    static Duration mod(Duration self, Duration other) { 
     def (long seconds, int nanos) = (self.totalNanos % other.totalNanos).divideAndRemainder(10g.pow(9)) 
     Duration.ofSeconds(seconds) + nanos.nanos 
    } 

    static load() { 
     Integer.mixin(TimeCategory) 
     ZonedDateTime.mixin(TimeCategory) 
     Duration.mixin(TimeCategory) 
    } 
} 

उदाहरण उपयोग:

// I prefer putting this in a start-up location and pollute the namespace rather than use 
// `use() {...}` 
TimeCategory.load() 

// Construct readable java 8 durations 
Duration d = 1.day + 2.hours + 3.minutes - 4.seconds 

// Easily construct dates relative to now 
ZonedDateTime yesterday = 1.day.ago 

// Of course, you can still call "unsugared" functions like truncatedTo for rounding down 
ZonedDateTime today = TimeCategory.now.truncatedTo(ChronoUnit.DAYS) 

// Converting between local and utc doesn't change the underlying instant 
assert 0.seconds == yesterday.utc - yesterday.local 

// Modulo arithmetic. Cool! 
assert 1.minute % 17.seconds == 9.seconds 

// Slightly cleaner date formatting 
println "Today in strange format ${today.toString('dd/yy/MM')}" 
+0

धन्यवाद ! निश्चित रूप से उपयोगी कोड के आसपास है। – Pierre

+0

डिफ़ॉल्ट टाइम श्रेणी की तरह .from.now का समर्थन करने के लिए आप इसे कैसे संशोधित करेंगे? – Ali

संबंधित मुद्दे