From 99e4de09807c8b1dbf143f982cac8cd6fa9d6874 Mon Sep 17 00:00:00 2001 From: "yennan.liu" Date: Tue, 27 Apr 2021 10:57:05 +0800 Subject: [PATCH] update event_time (SimpleEvent_V1.scala), add date utils --- .../com/yen/eventCreator/SimpleEvent_V1.scala | 9 +++++---- .../main/scala/com/yen/utils/DateUtils.scala | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 spark/src/main/scala/com/yen/utils/DateUtils.scala diff --git a/spark/src/main/scala/com/yen/eventCreator/SimpleEvent_V1.scala b/spark/src/main/scala/com/yen/eventCreator/SimpleEvent_V1.scala index 2487012..e0f40a9 100644 --- a/spark/src/main/scala/com/yen/eventCreator/SimpleEvent_V1.scala +++ b/spark/src/main/scala/com/yen/eventCreator/SimpleEvent_V1.scala @@ -3,7 +3,7 @@ package com.yen.eventCreator import java.io.PrintWriter import java.net.ServerSocket import java.util.Random -import java.time.LocalDateTime +import java.time.{LocalDateTime, LocalTime} import com.google.gson.Gson @@ -27,7 +27,7 @@ object SimpleEvent_V1 extends App { val listener = new ServerSocket(port) println(s"Listening on port: $port") - case class simpleEvent(id: Int, event_date: Long, msg: String) + case class simpleEvent(id: Int, event_date: String, msg: String) while (true) { val socket = listener.accept() @@ -40,7 +40,8 @@ object SimpleEvent_V1 extends App { val r = scala.util.Random val id = r.nextInt(10000000) - val event_date = System.currentTimeMillis + //val event_date = System.currentTimeMillis + val event_date = LocalDateTime.now() val someRandom = r.nextInt(10) @@ -50,7 +51,7 @@ object SimpleEvent_V1 extends App { case _ => "wazzup ?" } - val payload = new simpleEvent(id, event_date, msg) + val payload = new simpleEvent(id, event_date.toString, msg) Thread.sleep(sleepDelayMs) val msgDataJson = new Gson().toJson(payload) diff --git a/spark/src/main/scala/com/yen/utils/DateUtils.scala b/spark/src/main/scala/com/yen/utils/DateUtils.scala new file mode 100644 index 0000000..044a1f2 --- /dev/null +++ b/spark/src/main/scala/com/yen/utils/DateUtils.scala @@ -0,0 +1,18 @@ +package com.yen.utils + +import org.joda.time.{DateTime, DateTimeZone} +import org.joda.time.format.{DateTimeFormat, DateTimeFormatter, ISODateTimeFormat} + +object DateUtils { + // datetime format + val yyyyMMFormat: DateTimeFormatter = DateTimeFormat.forPattern("yyyyMM") + val yyyyMMddFormat: DateTimeFormatter = DateTimeFormat.forPattern("yyyyMMdd") + val yyyyMMddHHFormat: DateTimeFormatter = DateTimeFormat.forPattern("yyyyMMddHH") + + // time zone + val dateFormatTaipei: DateTimeFormatter = DateTimeFormat.forPattern("yyyyMMdd").withZone(DateTimeZone.forID("Asia/Taipei")) + + // iso time + val ISO8601Format: DateTimeFormatter = ISODateTimeFormat.dateTime() +} +