Date, Time & Locale Flashcards
Pkg a tridy pro date a time
Vlastnosti (2)
java.time
LocalDate
LocalTime
LocalDateTime
immutable, thread safe
Datumovo casove metody porovnavani (2)
dt.isBefore(), dt.isAfter()
Datumovo casove metody vytvareni - ted; konkretni datum
LocalDate.now()
LocalDate.of(2023, 12, 31)
Datumovo casove metody zmeny/ziskani minuty, hodiny, dne v mesici, … (4)
hours, secords, daysOfMonth, …:
dt.withXXX(), dt.getXXX()
dt.plusXXX(), dt.minusXXX()
LocalDateTime
- z date a time (2)
- na date, na time
LocalDateTime.of(date, time)
date.atTime(time)
ldt.toLocalDate()
ldt.toLocalTime()
Konstanty: leden
Month.JANUARY
Tridy pro:
casove rozmezi v nanosekundach
casove rozmezi v jednotkach
casove razitko
Duration - pro logy
Period - pro aplikace
Instant
Duration - metody porovnani
Duration.between(x, y)
Duration.isNegative(x)
Duration - prevod z minut a na minuty
Duration.ofMinutes(x)
duration.toMinutes()
Vytvoreni datumu se zonou
Zona:
Londyn
GMT + 5
systemova
ZonedDateTime.of(date, zoneId)
ZoneId.of(“Europe/London”)
ZoneId.of(“GMT+5”)
ZoneId.getSystemDefault()
Prevedeni datumu se zonou na jinou zonu
Ziskej offset oproti greenwichi
zonedDateTime.withZoneSameInstant(zoneId)
zonedDateTime.getOffset()
Lokale : pro Velkou Britanii - s librami vs s eurem
pro francouzsky langugage tag
pro francii konstantou
systemove
new Locale(“en”, “GB”)
new Locale(“en”, “GB”, “EURO”)
Locale.forLanguageTag(“fr-FR”)
Locale.FRANCE
Locale.getDefault()
Tvorba formatovace pro menu, pro procenta, pro cisla, pro cisla zkracena (napr 1000 = 1 k)
Co je vzdy parametrem?
NumberFormat.getCurrencyInstance()
NumberFormat.getPercentInstance() - zahazuje desetinky
NumberFormat.getNumberInstance()
NumberFormat.getCompactNumberInstance(locale, Style.SHORT)
locale
Prevod cisla na formatovany text
Prevod formatovaneho textu na cislo + co vraci
numberFormat.format(x)
numberFormat.parse(s) - vraci Wrapper class
Datum na String: trida, patterny pro: rok, mesic, den, den v tydnu
Escapovani patternu <rok> je rok, mesic je <mesic></mesic></rok>
DateTimeFormatter
yyyy,MM,dd,EEEE
“yyyy’ je rok, mesic je ‘MM”
DateTimeFormatter - typy formatovani
FormatStyle.SHORT, MEDIUM, LONG, FULL
DateTimeFormatter - vytvor pro pattern a pro datum(2)
DTF.ofPattern(pattern, locale)
DTF.ofLocalizedDateTime(formatStyle, locale)
DTF.ofLocalizedDate(formatStyle).localizedBy(locale)
Metody prevodu Stringu na datum a datumu na String
LocalDate.parse(“date”, dateTimeFormatter)
date.format(dateTimeFormatter)
Resource bundle - filename, filename pro locale en,GB
Kdy se pouzije ten bez locale
name.properties
name_en_GB.properties
default se pouzije, pokud nezadam locale, nebo klic neni v lokalizovanem bundlu
Radek resource bundlu pro pattern, ktery je <X> loves <Y>
Jak se rika takovym parametrum</Y></X>
pattern={0} loves {1}
substitution parameters
Otevri resource bundle resources/names/name_en_GB.properties
a ziskej hodnotu klice pattern
RB b = ResourceBundle.getBundle(resources.names.name, new Locale(“en”, “GB”))
b.getString(“pattern”);
Alternativni trida k ResourceBundle, v cem se lisi
Properties, muze cist z ruznych zdroju
Prevedeni patternu se substitution params na String
MessageFormat.format(“{0} loves {1}”, person0, person1);
Jak ziskam z datumu
- cislo mesice
- enum mesice
localDate.getMonthValue()
localDate.getMonth()
Jake cislo ma Calendar.APRIL konstanta?
Jake cislo ma duben v LocalDate?
4
3 (LocalDate pocita od 0)