java.util.GregorianCalendar Scala Examples
The following examples show how to use java.util.GregorianCalendar.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: CalendarFormats.scala From sjson-new with Apache License 2.0 | 6 votes |
package sjsonnew import java.util.{ Calendar, GregorianCalendar } import java.time._ import java.time.format.DateTimeFormatter trait CalendarFormats { self: IsoFormats => private val utc: ZoneId = ZoneId.of("UTC") implicit val localDateTimeStringIso: IsoString[LocalDateTime] = { IsoString.iso[LocalDateTime]( (ld: LocalDateTime) => { val datetimefmt = DateTimeFormatter.ISO_LOCAL_DATE_TIME ld.format(datetimefmt) }, (s: String) => { val datetimefmt = DateTimeFormatter.ISO_LOCAL_DATE_TIME LocalDateTime.parse(s, datetimefmt) }) } }
Example 2
Source File: httpserverplugin_staticfile.scala From scalabpe with Apache License 2.0 | 5 votes |
package scalabpe.plugin.http import java.io.File import java.net.URLEncoder import java.text.SimpleDateFormat import java.util.Calendar import java.util.GregorianCalendar import java.util.Locale import java.util.TimeZone import scala.collection.mutable.HashMap import org.jboss.netty.handler.codec.http.HttpHeaders import scalabpe.core.HashMapStringAny class StaticFilePlugin extends HttpServerPlugin with HttpServerStaticFilePlugin { val ETAG_TAG = "etag" val EXPIRE_TAG = "expire" val ATTACHMENT = "attachment" val FILENAME = "filename" val HTTP_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz"; val HTTP_DATE_GMT_TIMEZONE = "GMT"; val df_tl = new ThreadLocal[SimpleDateFormat]() { override def initialValue(): SimpleDateFormat = { val df = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US) df.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE)); df } } def generateStaticFile(serviceId: Int, msgId: Int, errorCode: Int, errorMessage: String, body: HashMapStringAny, pluginParam: String, headers: HashMap[String, String]): String = { if (body.ns(FILENAME) == "") { return null } val filename = body.ns(FILENAME) if (!new File(filename).exists()) { return null } if (body.ns(ETAG_TAG) != "") { headers.put("ETag", body.ns(ETAG_TAG)) } if (body.ns(EXPIRE_TAG) != "") { body.i(EXPIRE_TAG) match { case 0 | -1 => headers.put(HttpHeaders.Names.CACHE_CONTROL, "no-cache") case n => // seconds val time = new GregorianCalendar(); time.add(Calendar.SECOND, n); headers.put(HttpHeaders.Names.EXPIRES, df_tl.get.format(time.getTime())); headers.put(HttpHeaders.Names.CACHE_CONTROL, "max-age=" + n); } } val ext = parseExt(filename) if (ext != "") body.put("__file_ext__", ext) if (body.ns(ATTACHMENT, "1") == "1") { val filename = body.ns(FILENAME) val v = "attachment; filename=\"%s\"".format(URLEncoder.encode(parseFilename(filename), "UTF-8")) headers.put("Content-Disposition", v) } filename } def parseFilename(name: String): String = { val p = name.lastIndexOf("/") if (p < 0) return name name.substring(p + 1) } def parseExt(name: String): String = { val p = name.lastIndexOf(".") if (p < 0) return "" name.substring(p + 1).toLowerCase() } }
Example 3
Source File: TimeBasedDataService.scala From kafka-jdbc-connector with Apache License 2.0 | 5 votes |
package com.agoda.kafka.connector.jdbc.services import java.sql.{Connection, PreparedStatement, ResultSet, Timestamp} import java.util.{Date, GregorianCalendar, TimeZone} import com.agoda.kafka.connector.jdbc.JdbcSourceConnectorConstants import com.agoda.kafka.connector.jdbc.models.DatabaseProduct import com.agoda.kafka.connector.jdbc.models.DatabaseProduct.{MsSQL, MySQL} import com.agoda.kafka.connector.jdbc.models.Mode.TimestampMode import com.agoda.kafka.connector.jdbc.utils.DataConverter import org.apache.kafka.connect.data.Schema import org.apache.kafka.connect.source.SourceRecord import scala.collection.JavaConverters._ import scala.collection.mutable.ListBuffer import scala.util.Try case class TimeBasedDataService(databaseProduct: DatabaseProduct, storedProcedureName: String, batchSize: Int, batchSizeVariableName: String, timestampVariableName: String, var timestampOffset: Long, timestampFieldName: String, topic: String, keyFieldOpt: Option[String], dataConverter: DataConverter, calendar: GregorianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")) ) extends DataService { override def createPreparedStatement(connection: Connection): Try[PreparedStatement] = Try { val preparedStatement = databaseProduct match { case MsSQL => connection.prepareStatement(s"EXECUTE $storedProcedureName @$timestampVariableName = ?, @$batchSizeVariableName = ?") case MySQL => connection.prepareStatement(s"CALL $storedProcedureName (@$timestampVariableName := ?, @$batchSizeVariableName := ?)") } preparedStatement.setTimestamp(1, new Timestamp(timestampOffset), calendar) preparedStatement.setObject(2, batchSize) preparedStatement } override def extractRecords(resultSet: ResultSet, schema: Schema): Try[Seq[SourceRecord]] = Try { val sourceRecords = ListBuffer.empty[SourceRecord] var max = timestampOffset while (resultSet.next()) { dataConverter.convertRecord(schema, resultSet) map { record => val time = record.get(timestampFieldName).asInstanceOf[Date].getTime max = if(time > max) { keyFieldOpt match { case Some(keyField) => sourceRecords += new SourceRecord( Map(JdbcSourceConnectorConstants.STORED_PROCEDURE_NAME_KEY -> storedProcedureName).asJava, Map(TimestampMode.entryName -> time).asJava, topic, null, schema, record.get(keyField), schema, record ) case None => sourceRecords += new SourceRecord( Map(JdbcSourceConnectorConstants.STORED_PROCEDURE_NAME_KEY -> storedProcedureName).asJava, Map(TimestampMode.entryName -> time).asJava, topic, schema, record ) } time } else max } } timestampOffset = max sourceRecords } override def toString: String = { s""" |{ | "name" : "${this.getClass.getSimpleName}" | "mode" : "${TimestampMode.entryName}" | "stored-procedure.name" : "$storedProcedureName" |} """.stripMargin } }
Example 4
Source File: CronScheduleSpec.scala From sundial with MIT License | 5 votes |
package model import java.text.ParseException import java.util.GregorianCalendar import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatestplus.play.PlaySpec @RunWith(classOf[JUnitRunner]) class CronScheduleSpec extends PlaySpec { "Cron scheduler" should { "successfully parse cron entry for 10pm every day" in { val cronSchedule = CronSchedule("0", "22", "*", "*", "?") val date = new GregorianCalendar(2015, 10, 5, 21, 0).getTime val expectedNextDate = new GregorianCalendar(2015, 10, 5, 22, 0).getTime val nextDate = cronSchedule.nextRunAfter(date) nextDate must be(expectedNextDate) } "Throw exception on creation if cron schedlue is invalid" in { intercept[ParseException] { CronSchedule("0", "22", "*", "*", "*") } } } }
Example 5
Source File: DateHelper.scala From spark-vector with Apache License 2.0 | 5 votes |
package com.actian.spark_vector.test.util import java.sql.{ Date => sqlDate, Timestamp } import java.util.{ Calendar, Date, GregorianCalendar, TimeZone } object DateHelper { def dateFor(year: Int, month: Int, day: Int, hours: Int = 0, minutes: Int = 0, seconds: Int = 0, millis: Int = 0, tz: TimeZone = TimeZone.getDefault): Date = { import java.util.Calendar._ val cal = new GregorianCalendar() cal.setTimeZone(tz) if (year > 0) cal.set(YEAR, year) if (month >= 0) cal.set(MONTH, month) if (day > 0) cal.set(DAY_OF_MONTH, day) cal.set(HOUR_OF_DAY, hours) cal.set(MINUTE, minutes) cal.set(SECOND, seconds) cal.set(MILLISECOND, millis) cal.getTime } def timestampFor(year: Int, month: Int, day: Int, hours: Int = 0, minutes: Int = 0, seconds: Int = 0, millis: Int = 0, tz: TimeZone = TimeZone.getDefault): Timestamp = new Timestamp(dateFor(year, month, day, hours, minutes, seconds, millis, tz).getTime) def timeFor(hours: Int, minutes: Int, seconds: Int): Timestamp = { import java.util.Calendar val year = Calendar.getInstance().get(Calendar.YEAR) val month = Calendar.getInstance().get(Calendar.MONTH) val day = Calendar.getInstance().get(Calendar.DATE) timestampFor(year, month, day, hours, minutes, seconds) } def ansiDateFor(year: Int, month: Int, day: Int): sqlDate = new sqlDate(dateFor(year, month, day).getTime()) }
Example 6
Source File: RddToDataFrame.scala From spark-sframe with BSD 2-Clause "Simplified" License | 5 votes |
package org.apache.spark.turi import org.graphlab.create.GraphLabUtil import org.apache.spark.sql.{SQLContext, Row, DataFrame} import org.apache.spark.rdd.RDD import scala.collection.JavaConversions._ import org.apache.spark.sql.types._ import scala.collection.mutable.ListBuffer import scala.collection.mutable.ArrayBuffer import scala.collection.immutable.Map import java.util.HashMap import java.util.ArrayList import java.util.{Date,GregorianCalendar} import java.sql.Date object EvaluateRDD { def inferSchema(obj: Any): DataType = { if(obj.isInstanceOf[Int]) { IntegerType } else if(obj.isInstanceOf[String]) { StringType } else if(obj.isInstanceOf[Double]) { DoubleType } else if(obj.isInstanceOf[Long]) { LongType } else if(obj.isInstanceOf[Float]) { FloatType } else if(obj.isInstanceOf[Map[_,_]]) { MapType(inferSchema(obj.asInstanceOf[Map[_,_]].head._1),inferSchema(obj.asInstanceOf[Map[_,_]].head._2)) } else if(obj.isInstanceOf[java.util.HashMap[_,_]]) { MapType(inferSchema(obj.asInstanceOf[java.util.HashMap[_,_]].head._1),inferSchema(obj.asInstanceOf[java.util.HashMap[_,_]].head._2)) } else if(obj.isInstanceOf[Array[_]]) { ArrayType(inferSchema(obj.asInstanceOf[Array[_]](0))) } else if(obj.isInstanceOf[java.util.ArrayList[_]]) { ArrayType(inferSchema(obj.asInstanceOf[java.util.ArrayList[_]](0))) } else if(obj.isInstanceOf[java.util.GregorianCalendar]) { TimestampType } else if(obj.isInstanceOf[java.util.Date] || obj.isInstanceOf[java.sql.Date]) { DateType } else { StringType } } def toScala(obj: Any): Any = { if (obj.isInstanceOf[java.util.HashMap[_,_]]) { val jmap = obj.asInstanceOf[java.util.HashMap[_,_]] jmap.map { case (k,v) => toScala(k) -> toScala(v) }.toMap } else if(obj.isInstanceOf[java.util.ArrayList[_]]) { val buf = ArrayBuffer[Any]() val jArray = obj.asInstanceOf[java.util.ArrayList[_]] for(item <- jArray) { buf += toScala(item) } buf.toArray } else if(obj.isInstanceOf[java.util.GregorianCalendar]) { new java.sql.Timestamp(obj.asInstanceOf[java.util.GregorianCalendar].getTime().getTime()) } else { obj } } def toSparkDataFrame(sqlContext: SQLContext, rdd: RDD[java.util.HashMap[String,_]]): DataFrame = { val scalaRDD = rdd.map(l => toScala(l)) val rowRDD = scalaRDD.map(l => Row.fromSeq(l.asInstanceOf[Map[_,_]].values.toList)) var sample_data: java.util.HashMap[String,_] = rdd.take(1)(0) var schema_list: ListBuffer[StructField] = new ListBuffer[StructField]() for ((name,v) <- sample_data) { schema_list.append(StructField(name,inferSchema(v))) } sqlContext.createDataFrame(rowRDD,StructType(schema_list)) } }