I have a date of string type "2020-08-10". How to convert my string date to this format Monday 08 2020 in Kotlin?
Code:
var parsedDate = LocalDate.parse("2020-08-10", DateTimeFormatter.ofPattern("yyyy-MM-dd"))
println("2020-08-10 : "+parsedDate.dayOfWeek.toString()+" "+parsedDate.monthValue+" "+parsedDate.year)
Output:
2020-08-10 : MONDAY 8 2020
For API 26 Below:
val parser = SimpleDateFormat("yyyy-MM-dd")
val formatter = SimpleDateFormat("EEEE MM yyyy")
val formattedDate = formatter.format(parser.parse("2020-08-10"))
println("2020-08-10 : "+formattedDate)
Output:
2020-08-10 : MONDAY 8 2020
The EEEE prints the name of day
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
val str = "2020-08-10"
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val dateTime = LocalDate.parse(str, formatter)
println(dateTime.format(DateTimeFormatter.ofPattern("EEEE MM yyyy ")))
Output
Monday 08 2020
It's an alternative solution to your question.
Related
It formats 2020-01-27 00:00:00 of type timestamp as 2020-01-27 12:00:00 instead of 2020-01-27 00:00:00
import spark.sqlContext.implicits._
import java.sql.Timestamp
import org.apache.spark.sql.functions.typedLit
scala> val stamp = typedLit(new Timestamp(1580105949000L))
stamp: org.apache.spark.sql.Column = TIMESTAMP('2020-01-27 00:19:09.0')
scala> var df_test = Seq(5).toDF("seq").select(
| stamp.as("unixtime"),
| date_trunc("HOUR", stamp).as("date_trunc"),
| date_format(date_trunc("HOUR", stamp), "yyyy-MM-dd hh:mm:ss").as("hour")
| )
df_test: org.apache.spark.sql.DataFrame = [unixtime: timestamp, date_trunc: timestamp ... 1 more field]
scala> df_test.show
+-------------------+-------------------+-------------------+
| unixtime| date_trunc| hour|
+-------------------+-------------------+-------------------+
|2020-01-27 00:19:09|2020-01-27 00:00:00|2020-01-27 12:00:00|
+-------------------+-------------------+-------------------+
Your pattern should be yyyy-MM-dd HH:mm:ss.
date_format, according to its documentation, uses specifiers supported by java.text.SimpleDateFormat:
Converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument.
See SimpleDateFormat for valid date and time format patterns.
SimpleDateFormat's documentation can be found here
hh is used for "Hour in am/pm (1-12)". You're looking for the hour in day specifier, which is HH.
I have an initial date as a String which I need to convert to date with a specific format. I tried to defined date format in a string and parse it, then I formatted it to the desired format. The problem is that I need a date as a final result.
Here is the code I used:
def parseDateToOriginal(date: String): String = {
val initialDateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy")
val finalDateFormat = new SimpleDateFormat("yyyy-mm-dd")
val result = finalDateFormat.format(initialDateFormat.parse(date))
result
}
So I need Date as the return type for this method. I tried to parse the result string to get a proper date but for some reason, the result defaults back to the original date format. How can I fix this problem?
Here is how I tried to parse it again:
val parsedDate = new SimpleDateFormat("yyyy-mm-dd").parse(parseDateToOriginal(date))
The result is of the pattern "EEE MMM dd hh:mm:ss zzz yyyy"
First, SimpleDate is old and outdated. The current java.time library is recommended.
Next, if you need to return a Date then parse the input and return the Date. You need to format a Date only when you present it, i.e. change it to a String.
import java.time.LocalDate
import java.time.format.DateTimeFormatter
def parseToDate(date: String): LocalDate =
LocalDate.parse(date
,DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss zzz yyyy"))
Try
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
def parseDateToOriginal(date: String): String = {
LocalDateTime
.parse(date, DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy"))
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
}
which outputs
parseDateToOriginal("Thu Jun 18 20:56:02 EDT 2009") // res2: String = 2009-06-18
Note you have a bug in the format of finalDateFormat
val finalDateFormat = new SimpleDateFormat("yyyy-mm-dd")
You are using lowercase mm in the month positions, but should be upper case MM. Lowercase mm represents minutes, so it would erroneously result in res2: String = 2009-56-18 as outputs.
So i have dd/mm/yyyy date format and i want to convert it into date dd MMM, yyyy format.
This is what i have try:
val s: String = "27/04/2014"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy")
val date2 = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("dd MMM, yyyy")
println(df.format(date2))
Result:
27 Jan, 2014 res0: Unit = ()
change the
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy")
to
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy")
check this out for all the codes:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
I have a date in this format:
"Fri Oct 31 15:07:24 2014"
and I tried to parse it as I parsed a lot of other dates until now.
I figured out his format is this one (consulting the Java docs (http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)):
"EEE MMM dd HH:mm:ss yyyy"
I tried from the scala REPL running this commands:
scala> import java.text.SimpleDateFormat
import java.text.SimpleDateFormat
scala> import java.util.Date
import java.util.Date
scala> val sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy")
sdf: java.text.SimpleDateFormat = java.text.SimpleDateFormat#2219f5ee
scala> sdf.parse("Fri Oct 31 15:07:24 2014")
java.text.ParseException: Unparseable date: "Fri Oct 31 15:07:24 2014"
at java.text.DateFormat.parse(DateFormat.java:366)
... 33 elided
but as you can see I get a ParseException.
I tried removing the first part of the Date (and the pattern) like this:
"dd HH:mm:ss yyyy" -> "31 15:07:24 2014"
and all went fine, but when I try to add EEE or MMM I get the ParseException.
I also tried the pattern shown in the java docs that uses EEE and it fails too on my machine.
I've got Java 8 and scala 2.11.1
Thank you in advance.
The problem was the Locale, by Default SimpleDateFormat takes the default Locale of the machine that's running, to set a different Locale (the "en" locale in this example) for the SimpleDateFormat you need to instantiate it this way:
new SimpleDateFormat(format,java.util.Locale.forLanguageTag("en"))
def run : List[Map[String,Any]]={
val startTime = "19-9-2014 23:00"
val endTime = "19-9-2014 13:15"
val df: SimpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm")
val calendar: Calendar = Calendar.getInstance
val finalTime : Date = df.parse(endTime)
var firstTime=startTime
var timeValuePair = List[Map[String , Any]]()
var last = new Date()
do {
val first: Date = df.parse(firstTime)
calendar.setTime(first)
calendar.add(Calendar.MINUTE, 5)
last = calendar.getTime
val (repAlert, slowQueryAlert, statusAlert) = AggregateAlert.test(first.toString, last.toString)
timeValuePair=Map("repAlert"->repAlert.toString,"slowQueryAlert"->slowQueryAlert.toString,"statusAlert"->statusAlert.toString) :: timeValuePair
firstTime=last.toString
}while(last.compareTo(finalTime)<0)
I basically need to send date and time in the format of dd-mmm-yyyy hh:mm to the function AggregateAlert for every 5 minutes interval between the startTime and endTime.
However with my code I am getting "first" as "Fri Sep 19 11:00:00 IST 2014" and "last" as "Fri Sep 19 19:01:21 IST 2014". However I want the same format as dd-mmm-yyyy. Is there anyway to convert this to the required format?
Thanks in advance!
Try :
val (repAlert, slowQueryAlert, statusAlert) =
AggregateAlert.test(df.format(first), df.format(last))