本文整理汇总了Scala中java.time.temporal.ChronoField类的典型用法代码示例。如果您正苦于以下问题:Scala ChronoField类的具体用法?Scala ChronoField怎么用?Scala ChronoField使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ChronoField类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Formatters
//设置package包名称以及导入依赖的类
package io.gustavoamigo.quill.pgsql.encoding.datetime
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
import java.time.temporal.ChronoField
private[datetime] object Formatters {
val bpDateFormatter = DateTimeFormatter.ISO_LOCAL_DATE
val bpTimeFormatter = DateTimeFormatter.ISO_LOCAL_TIME
val bpTzTimeFormatter =
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ofPattern("HH:mm:ss"))
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND,0,6,true)
.optionalEnd()
.appendOffset("+HH:mm","+00")
.toFormatter()
val bpTzDateTimeFormatter =
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND,0,6,true)
.optionalEnd()
.appendOffset("+HH:mm","+00")
.toFormatter()
}
示例2: offset
//设置package包名称以及导入依赖的类
package time
import java.time._
import java.time.temporal.ChronoField
sealed trait Offset {
def offset: ZoneOffset
}
sealed trait UTC extends Offset {
def offset = ZoneOffset.UTC
}
sealed trait Ja extends Offset {
def offset = ZoneOffset.ofHours(9)
}
sealed trait DateTime {
def toZoned: ZonedDateTime
private lazy val instant = toZoned.toInstant
def toEpochMilli: Long = instant.toEpochMilli
}
case class UTCDateTime private (toZoned: ZonedDateTime) extends DateTime
object UTCDateTime extends UTC {
def apply()(implicit clockProvider: ClockProvider): UTCDateTime = apply(clockProvider.now)
def apply(timestamp: java.sql.Timestamp): UTCDateTime = apply(timestamp.getTime)
def apply(epochMilli: Long): UTCDateTime = apply(Instant.ofEpochMilli(epochMilli))
def apply(instant: Instant): UTCDateTime = UTCDateTime(instant.atZone(offset))
}
sealed trait Time {
val toOffsetTime: OffsetTime
def toMillis: Long = toOffsetTime.getLong(ChronoField.MILLI_OF_DAY)
}
case class UTCTime private (toOffsetTime: OffsetTime) extends Time
object UTCTime extends UTC {
def apply()(implicit clockProvider: ClockProvider): UTCTime = apply(clockProvider.now)
def apply(epochMilli: Long): UTCTime = apply(Instant.ofEpochMilli(epochMilli))
def apply(instant: Instant): UTCTime = UTCTime(OffsetTime.ofInstant(instant, offset))
}
case class JaTime private (toOffsetTime: OffsetTime) extends Time
object JaTime extends Ja {
def apply()(implicit clockProvider: ClockProvider): JaTime = apply(clockProvider.now)
def apply(epochMilli: Long): JaTime = apply(Instant.ofEpochMilli(epochMilli))
def apply(instant: Instant): JaTime = JaTime(OffsetTime.ofInstant(instant, offset))
}
case class Duration private (asJava: java.time.Duration) {
def toMillis: Long = asJava.toMillis
}
object Duration {
def apply(start: UTCDateTime, end: UTCDateTime): Duration =
Duration(java.time.Duration.between(start.toZoned, end.toZoned))
}
示例3: offset
//设置package包名称以及导入依赖的类
package time
import java.time._
import java.time.temporal.ChronoField
sealed trait Offset {
protected def offset: ZoneOffset
}
sealed trait UTCOffset extends Offset {
def offset: ZoneOffset = ZoneOffset.UTC
}
sealed trait JaOffset extends Offset {
def offset: ZoneOffset = ZoneOffset.ofHours(9)
}
sealed trait Time {
val toOffsetTime: OffsetTime
def toMillis: Long = toOffsetTime.getLong(ChronoField.MILLI_OF_DAY)
}
case class UTCTime private (toOffsetTime: OffsetTime) extends Time
object UTCTime extends UTCOffset {
def apply()(implicit clockProvider: ClockProvider): UTCTime = UTCTime(clockProvider.now)
def apply(epochMilli: Long): UTCTime = UTCTime(Instant.ofEpochMilli(epochMilli))
def apply(instant: Instant): UTCTime = UTCTime(OffsetTime.ofInstant(instant, offset))
}
case class JaTime private (toOffsetTime: OffsetTime) extends Time
object JaTime extends JaOffset {
def apply()(implicit clockProvider: ClockProvider): JaTime = JaTime(clockProvider.now)
def apply(epochMilli: Long): JaTime = JaTime(Instant.ofEpochMilli(epochMilli))
def apply(instant: Instant): JaTime = JaTime(OffsetTime.ofInstant(instant, offset))
}
示例4: fieldsToChange
//设置package包名称以及导入依赖的类
package com.cogpp.datetime.generator
import java.time.temporal.{ChronoField, Temporal, TemporalField}
import java.time.{OffsetDateTime, ZoneOffset}
import org.scalacheck.Shrink
val LOCAL_EPOCH = OffsetDateTime.of(1970,1,1,0,0,0,0,ZoneOffset.UTC)
def fieldsToChange:List[TemporalField] = List(
ChronoField.YEAR,
ChronoField.MONTH_OF_YEAR,
ChronoField.DAY_OF_MONTH,
ChronoField.HOUR_OF_DAY,
ChronoField.MINUTE_OF_HOUR,
ChronoField.SECOND_OF_MINUTE,
ChronoField.MILLI_OF_SECOND)
def timeStream(time:OffsetDateTime):Stream[OffsetDateTime] = {
time match {
case LOCAL_EPOCH => Stream.empty
case t if t.getOffset != ZoneOffset.UTC &&
t.getYear < OffsetDateTime.MAX.getYear &&
t.getYear > OffsetDateTime.MIN.getYear => nextStreamItem(t, _.withOffsetSameInstant(ZoneOffset.UTC), timeStream)
case t => nextStreamItem(t, stepCloser, timeStream)
}
}
def nextStreamItem(time: OffsetDateTime,curStep:OffsetDateTime=>OffsetDateTime, nextStep:OffsetDateTime=>Stream[OffsetDateTime]):Stream[OffsetDateTime] = {
val curTime=curStep(time)
Stream.cons(curTime, nextStep(curTime))
}
def stepCloser(time:OffsetDateTime):OffsetDateTime = {
firstDifferentField(time).map(f => halveField(time,f)).getOrElse(LOCAL_EPOCH)
}
def firstDifferentField(time:OffsetDateTime):Option[TemporalField] = {
val fields:List[(Int, Int, TemporalField)] = (fieldsToChange map { f=> time.get(f)} , fieldsToChange map {f => LOCAL_EPOCH.get(f)} , fieldsToChange).zipped.toList
fields.find( f => f._1 != f._2 ).map(_._3)
}
def halveField(time:OffsetDateTime,field:TemporalField): OffsetDateTime = {
time.`with`(field,((time.get(field) - LOCAL_EPOCH.get(field)) / 2) + LOCAL_EPOCH.get(field))
}
implicit def shrinkOffsetDateTime:Shrink[OffsetDateTime] = Shrink { (time: OffsetDateTime) => timeStream(time) }
}
示例5: localDateTime
//设置package包名称以及导入依赖的类
package dtc.instances
import java.time.temporal.{ChronoField, ChronoUnit}
import java.time._
import dtc._
object localDateTime {
implicit val localDateTimeDTC: Local[LocalDateTime] =
new Local[LocalDateTime] {
def compare(x: LocalDateTime, y: LocalDateTime): Int = x.compareTo(y)
def date(x: LocalDateTime): LocalDate = x.toLocalDate
def time(x: LocalDateTime): LocalTime = x.toLocalTime.truncatedTo(ChronoUnit.MILLIS)
def of(date: LocalDate, time: LocalTime): LocalDateTime =
LocalDateTime.of(date, time.truncatedTo(ChronoUnit.MILLIS))
def of(
year: Int, month: Int, day: Int,
hour: Int, minute: Int, second: Int, millisecond: Int): LocalDateTime =
LocalDateTime.of(year, month, day, hour, minute, second, millisToNanos(millisecond))
def plus(x: LocalDateTime, d: Duration): LocalDateTime = x.plus(truncateToMillis(d))
def minus(x: LocalDateTime, d: Duration): LocalDateTime = x.minus(truncateToMillis(d))
def plusDays(x: LocalDateTime, days: Int): LocalDateTime = x.plusDays(days.toLong)
def plusMonths(x: LocalDateTime, months: Int): LocalDateTime = x.plusMonths(months.toLong)
def plusYears(x: LocalDateTime, years: Int): LocalDateTime = x.plusYears(years.toLong)
def withYear(x: LocalDateTime, year: Int): LocalDateTime = x.withYear(year)
def withMonth(x: LocalDateTime, month: Int): LocalDateTime = x.withMonth(month)
def withDayOfMonth(x: LocalDateTime, dayOfMonth: Int): LocalDateTime = x.withDayOfMonth(dayOfMonth)
def withHour(x: LocalDateTime, hour: Int): LocalDateTime = x.withHour(hour)
def withMinute(x: LocalDateTime, minute: Int): LocalDateTime = x.withMinute(minute)
def withSecond(x: LocalDateTime, second: Int): LocalDateTime = x.withSecond(second)
def withMillisecond(x: LocalDateTime, millisecond: Int): LocalDateTime = x.withNano(millisToNanos(millisecond))
def dayOfWeek(x: LocalDateTime): DayOfWeek = x.getDayOfWeek
def dayOfMonth(x: LocalDateTime): Int = x.getDayOfMonth
def month(x: LocalDateTime): Int = x.getMonthValue
def year(x: LocalDateTime): Int = x.getYear
def millisecond(x: LocalDateTime): Int = x.get(ChronoField.MILLI_OF_SECOND)
def second(x: LocalDateTime): Int = x.getSecond
def minute(x: LocalDateTime): Int = x.getMinute
def hour(x: LocalDateTime): Int = x.getHour
def yearsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.YEARS)
def monthsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MONTHS)
def daysUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.DAYS)
def hoursUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.HOURS)
def minutesUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MINUTES)
def secondsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.SECONDS)
def millisecondsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MILLIS)
}
}
示例6: zonedDateTime
//设置package包名称以及导入依赖的类
package dtc.instances
import java.time.temporal.{ChronoField, ChronoUnit}
import java.time._
import dtc._
import dtc.syntax.timeZone._
object zonedDateTime {
implicit val zonedDateTimeDTC: Zoned[ZonedDateTime] =
new Zoned[ZonedDateTime] {
def of(date: LocalDate, time: LocalTime, zone: TimeZoneId): ZonedDateTime =
ZonedDateTime.of(date, time.truncatedTo(ChronoUnit.MILLIS), zone.zoneId)
def withZoneSameInstant(x: ZonedDateTime, zone: TimeZoneId): ZonedDateTime = x.withZoneSameInstant(zone.zoneId)
def withZoneSameLocal(x: ZonedDateTime, zone: TimeZoneId): ZonedDateTime = x.withZoneSameLocal(zone.zoneId)
def zone(x: ZonedDateTime): TimeZoneId = TimeZoneId(x.getZone.getId)
def date(x: ZonedDateTime): LocalDate = x.toLocalDate
def time(x: ZonedDateTime): LocalTime = x.toLocalTime.truncatedTo(ChronoUnit.MILLIS)
def compare(x: ZonedDateTime, y: ZonedDateTime): Int = x.compareTo(y)
def plus(x: ZonedDateTime, d: Duration): ZonedDateTime = x.plus(truncateToMillis(d))
def minus(x: ZonedDateTime, d: Duration): ZonedDateTime = x.minus(truncateToMillis(d))
def plusDays(x: ZonedDateTime, days: Int): ZonedDateTime = x.plusDays(days.toLong)
def plusMonths(x: ZonedDateTime, months: Int): ZonedDateTime = x.plusMonths(months.toLong)
def plusYears(x: ZonedDateTime, years: Int): ZonedDateTime = x.plusYears(years.toLong)
def withYear(x: ZonedDateTime, year: Int): ZonedDateTime = x.withYear(year)
def withMonth(x: ZonedDateTime, month: Int): ZonedDateTime = x.withMonth(month)
def withDayOfMonth(x: ZonedDateTime, dayOfMonth: Int): ZonedDateTime = x.withDayOfMonth(dayOfMonth)
def withHour(x: ZonedDateTime, hour: Int): ZonedDateTime = x.withHour(hour)
def withMinute(x: ZonedDateTime, minute: Int): ZonedDateTime = x.withMinute(minute)
def withSecond(x: ZonedDateTime, second: Int): ZonedDateTime = x.withSecond(second)
def withMillisecond(x: ZonedDateTime, millisecond: Int): ZonedDateTime = x.withNano(millisToNanos(millisecond))
def offset(x: ZonedDateTime): Offset = Offset(x.getOffset.getTotalSeconds)
def dayOfWeek(x: ZonedDateTime): DayOfWeek = x.getDayOfWeek
def dayOfMonth(x: ZonedDateTime): Int = x.getDayOfMonth
def month(x: ZonedDateTime): Int = x.getMonthValue
def year(x: ZonedDateTime): Int = x.getYear
def millisecond(x: ZonedDateTime): Int = x.get(ChronoField.MILLI_OF_SECOND)
def second(x: ZonedDateTime): Int = x.getSecond
def minute(x: ZonedDateTime): Int = x.getMinute
def hour(x: ZonedDateTime): Int = x.getHour
def yearsUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.YEARS)
def monthsUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.MONTHS)
def daysUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.DAYS)
def hoursUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.HOURS)
def minutesUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.MINUTES)
def secondsUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.SECONDS)
def millisecondsUntil(x: ZonedDateTime, until: ZonedDateTime): Long = x.until(until, ChronoUnit.MILLIS)
}
}
示例7: localDateTimeAsZoned
//设置package包名称以及导入依赖的类
package dtc.instances
import java.time._
import java.time.temporal.{ChronoField, ChronoUnit}
import dtc._
object localDateTimeAsZoned {
implicit val localDateTimeIsZoned: Zoned[LocalDateTime] =
new Zoned[LocalDateTime] {
def compare(x: LocalDateTime, y: LocalDateTime): Int = x.compareTo(y)
def date(x: LocalDateTime): LocalDate = x.toLocalDate
def time(x: LocalDateTime): LocalTime = x.toLocalTime.truncatedTo(ChronoUnit.MILLIS)
def plus(x: LocalDateTime, d: Duration): LocalDateTime = x.plus(truncateToMillis(d))
def minus(x: LocalDateTime, d: Duration): LocalDateTime = x.minus(truncateToMillis(d))
def plusDays(x: LocalDateTime, days: Int): LocalDateTime = x.plusDays(days.toLong)
def plusMonths(x: LocalDateTime, months: Int): LocalDateTime = x.plusMonths(months.toLong)
def plusYears(x: LocalDateTime, years: Int): LocalDateTime = x.plusYears(years.toLong)
def withYear(x: LocalDateTime, year: Int): LocalDateTime = x.withYear(year)
def withMonth(x: LocalDateTime, month: Int): LocalDateTime = x.withMonth(month)
def withDayOfMonth(x: LocalDateTime, dayOfMonth: Int): LocalDateTime = x.withDayOfMonth(dayOfMonth)
def withHour(x: LocalDateTime, hour: Int): LocalDateTime = x.withHour(hour)
def withMinute(x: LocalDateTime, minute: Int): LocalDateTime = x.withMinute(minute)
def withSecond(x: LocalDateTime, second: Int): LocalDateTime = x.withSecond(second)
def withMillisecond(x: LocalDateTime, millisecond: Int): LocalDateTime = x.withNano(millisToNanos(millisecond))
def dayOfWeek(x: LocalDateTime): DayOfWeek = x.getDayOfWeek
def dayOfMonth(x: LocalDateTime): Int = x.getDayOfMonth
def month(x: LocalDateTime): Int = x.getMonthValue
def year(x: LocalDateTime): Int = x.getYear
def millisecond(x: LocalDateTime): Int = x.get(ChronoField.MILLI_OF_SECOND)
def second(x: LocalDateTime): Int = x.getSecond
def minute(x: LocalDateTime): Int = x.getMinute
def hour(x: LocalDateTime): Int = x.getHour
def yearsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.YEARS)
def monthsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MONTHS)
def daysUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.DAYS)
def hoursUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.HOURS)
def minutesUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MINUTES)
def secondsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.SECONDS)
def millisecondsUntil(x: LocalDateTime, until: LocalDateTime): Long = x.until(until, ChronoUnit.MILLIS)
def of(date: LocalDate, time: LocalTime, zone: TimeZoneId): LocalDateTime = LocalDateTime.of(date, time)
def withZoneSameInstant(x: LocalDateTime, zone: TimeZoneId): LocalDateTime = x
def withZoneSameLocal(x: LocalDateTime, zone: TimeZoneId): LocalDateTime = x
def zone(x: LocalDateTime): TimeZoneId = TimeZoneId.UTC
def offset(x: LocalDateTime): Offset = Offset(0)
}
}
示例8: genDateTimeFromSameOffsetPeriod
//设置package包名称以及导入依赖的类
package dtc.tests
import java.time.temporal.ChronoField
import java.time.temporal.ChronoUnit._
import java.time.{Duration, LocalDate, LocalTime}
import dtc.TimeZoneId
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import org.scalatest.{FunSuiteLike, Matchers}
import org.typelevel.discipline.scalatest.Discipline
trait DTCSuite extends FunSuiteLike
with Matchers
with GeneratorDrivenPropertyChecks
with Discipline {
override implicit val generatorDrivenConfig: PropertyCheckConfiguration = PropertyCheckConfiguration(
minSuccessful = 100
)
private val nanoOfDayRange = ChronoField.NANO_OF_DAY.range()
val genLocalTime: Gen[LocalTime] =
Gen.choose(nanoOfDayRange.getMinimum, nanoOfDayRange.getMaximum).map(LocalTime.ofNanoOfDay)
implicit val arbLocalTime: Arbitrary[LocalTime] = Arbitrary(genLocalTime)
val genDuration: Gen[Duration] =
Gen.choose(Long.MinValue / 1000, Long.MaxValue / 1000)
.map(l => Duration.of(l, MILLIS))
implicit val arbDuration = Arbitrary(genDuration)
def genDateTimeFromSameOffsetPeriod(period: SameZoneOffsetPeriod): Gen[(LocalDate, LocalTime, TimeZoneId)] = for {
date <- Gen.choose(period.startDate.toEpochDay + 1L, period.endDate.toEpochDay - 1L).map(LocalDate.ofEpochDay)
timeBounds <- Gen.const(
if (date == period.startDate && date == period.endDate) (period.startTime, period.endTime)
else if (date == period.startDate) (period.startTime, LocalTime.MAX)
else if (date == period.endDate) (LocalTime.MAX, period.endTime)
else (LocalTime.MIN, LocalTime.MAX)
)
time <- Gen.choose(timeBounds._1.toNanoOfDay, timeBounds._2.toNanoOfDay).map(LocalTime.ofNanoOfDay)
} yield (date, time, period.zone)
}
示例9: D
//设置package包名称以及导入依赖的类
package dtc.laws
import java.time.temporal.{ChronoField, ChronoUnit}
import java.time.{LocalDate, LocalTime}
import cats.kernel.laws._
import dtc.Local
import dtc.syntax.local._
import org.scalacheck.{Gen, Prop}
import org.scalacheck.Prop.forAll
trait LocalDateTimeLaws[A] {
implicit def D: Local[A]
val genLocalDate: Gen[LocalDate]
val genLocalTime: Gen[LocalTime]
def constructorConsistency: Prop = forAll(genLocalDate, genLocalTime) { (date: LocalDate, time: LocalTime) =>
val dt = D.of(date, time)
(dt.date ?== date) && (dt.time ?== time.truncatedTo(ChronoUnit.MILLIS))
}
def plainConstructorConsistency: Prop = forAll(genLocalDate, genLocalTime) { (date: LocalDate, time: LocalTime) =>
val dt = D.of(
date.getYear, date.getMonthValue, date.getDayOfMonth,
time.getHour, time.getMinute, time.getSecond, time.get(ChronoField.MILLI_OF_SECOND))
(dt.date ?== date) && (dt.time ?== time.truncatedTo(ChronoUnit.MILLIS))
}
}
object LocalDateTimeLaws {
def apply[A](
gLocalTime: Gen[LocalTime],
gLocalDate: Gen[LocalDate])(
implicit ev: Local[A]): LocalDateTimeLaws[A] = new LocalDateTimeLaws[A] {
def D: Local[A] = ev
val genLocalDate: Gen[LocalDate] = gLocalDate
val genLocalTime: Gen[LocalTime] = gLocalTime
}
}
示例10: LevelDBStreamTransactionImplTest
//设置package包名称以及导入依赖的类
package teleporter.stream.integration.transaction
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoField
import org.scalatest.FunSuite
class LevelDBStreamTransactionImplTest extends FunSuite {
test("LocalDateTime") {
val temporalAccessor = DateTimeFormatter.ISO_INSTANT.parse("2015-08-07T18:13:00Z")
val seconds = temporalAccessor.getLong(ChronoField.INSTANT_SECONDS)
println(seconds)
}
test("symbol") {
// val map = Uri("http://localhost:8080?name=11&name2=2").query.toMap[Symbol, String]
// println(map)
}
}