Convert String "22/08/2013" to date format 2013/08/22 - scala

This is my code in scala
var s:String ="22/08/2013"
var simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
var date:Date = simpleDateFormat.parse(s);
println(date)
the date is not change. the format of date same as 22/08/2013 there is no change to 2013/08/22
How to change the format dd/mm/yyyy to yyyy/mm/dd in scala

You need to define a SimpleDateFormat which first parses your string and get Date from it. And then convert it to whatever format.
var s:String ="22/08/2013"
var simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
var date:Date = simpleDateFormat.parse(s);
val ans = new SimpleDateFormat("yyyy/mm/dd").format(date)
println(ans)

I tried this it works for me.
val s: String = "22/08/2013"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy")
val date = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("yyyy/mm/dd")
println(df.format(date))

There's a subtle error in John Vishal's answer:
mm defines 08 as minutes, not as month. Use MM instead.
Corrected code:
val s = "2013_08_14"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy_MM_dd")
val date = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("dd-MMM-yyyy")
println(df.format(date))

Related

How to write functions for CurrentYear and PreviousYear in scala?

I have this scenario where I am fetching for CurrentDate. Adding to that I also want to fetch data from CurrentYear and PreviousYear
def getCurrentDate: String = {
val cal = Calendar.getInstance()
cal.add(Calendar.DATE, amount = 0)
new SimpleDateFormat(pattern = "yyyyMMdd").format(cal.getTime())
}
def getCurrentYear: String = {
val cal = Calendar.getInstance()
cal.add(Calendar.DATE, amount = 0)
new SimpleDateFormat(pattern = "yyyyMMdd").format(cal.getTime())
}
I am actually figuring out on how to write this simple function
First of all, you need to know that the current year depends on your timezone.
So you first need to choose the timezone for which you want to know the year... specially in edge scenarios like new years eve.
That being said, you can do this:
import java.time._
val yourTZ: ZoneId = ...
def currentDate = Instant.now.atZone(yourTZ).toString.take(10) // because the toString uses ISO formatting
def currentYear = currentDate.take(4)
def previousYear = (currentYear.toInt-1).toString

Convert string to date in kotlin

could you help me?
I get a string of type 01092020, how could I convert to date type in 01/09 format, just month and year?
You should use DateTimeFormatter as follows:
fun main(args: Array<String>) {
val dateString = "01092020"
val readingFormatter = DateTimeFormatter.ofPattern("ddMMyyyy")
val date = LocalDate.parse(dateString, readingFormatter)
val writingFormatter = DateTimeFormatter.ofPattern("MM/yy")
val formattedDate = date.format(writingFormatter)
print(formattedDate)
}
You can substring your string and break into month and year e.g DDMMYYYY
val dateStr = "01092020"
val dd = dateStr.substring(0..1)
val mm = dateStr.substring(2..3)
val yyyy = dateStr.substring(4 until dateStr.length)
val DDYY = "$dd/$yyyy"
val mmYYYY = "$mm/$yyyy"

Subtract days from a date in the form of a string

I have a date in the form of a string like below
"08/08/2017 11:43"
I was trying to subtract a day from the above date string so the final output would be "07/08/2017 11:43"
I tried it with the below code
val x = "18/8/2017 11:43"
val formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm");
val dt = formatter.parseDateTime(x);
println(dt.minusDays(1))
But, the output that I got is 2017-08-17T11:43:00.000-07:00
Is there a better way of doing this?
You need the formatter for both parsing and formatting for output.
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
val x = "18/8/2017 11:43"
val formatter = DateTimeFormatter.ofPattern("d/M/yyyy HH:mm")
val dt = LocalDateTime.parse(x, formatter)
val res = dt.minusDays(1).format(formatter) //res: String = 17/8/2017 11:43

Date/Time formatting Scala

I'm trying to assert date and time displayed on the page
Problem is it's returning value of "2017-03-11T09:00" instead of "2017-03-11 09:00:00" and I'm confused why as the pattern = yyyy-MM-dd HH:mm:ss
Any ideas?
def getDate :String = {
val timeStamp = find(xpath("//*[#id=\"content\"]/article/div/div/table/tbody/tr[5]/td/div/p[4]")).get.underlying.getText
val stripDate: Array[String] = timeStamp.split("Timestamp:\n")
stripDate(1)
}
def datePattern(date: String): LocalDateTime = {
val pattern: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val result = LocalDateTime.parse(date, pattern)
result
}
def checkDatePattern() = datePattern(getDate).toString shouldBe getDate
The DateTimeFormatter only gets used for the parse operation. It doesn't influence the result of toString. If you want to convert your LocalDateTime to a String in a certain format you have to call
date.format(pattern)
I've managed to get the result I wanted by just deleting some parts of the code. As long as the date is in displayed in the correct format, the test passes if it's displayed in an incorrect format it fails, which is good enough for me. Thanks for your input. CASE CLOSED
def getDate :String = {
val timeStamp = find(xpath("//*[#id=\"content\"]/article/div/div/table/tbody/tr[5]/td/div/p[4]")).get.underlying.getText
val stripDate: Array[String] = timeStamp.split("Timestamp:\n")
stripDate(1)
}
def datePattern(date: String): LocalDateTime = {
val pattern: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
LocalDateTime.parse(date, pattern)
}
def checkDatePattern() = datePattern(getDate)

How to find out adjusted hour in scala

I have some examples:
case1. val date1 = new DateTime("2015-08-03T04:59:00.000")
output: new DateTime("2015-08-03T04:00:00.000")
case2. val date2 = new DateTime("2015-08-03T04:15:00.000")
output: new DateTime("2015-08-03T04:00:00.000")
means for any datetime if the time is more the 1 minute output should be start of hour. Example for day: datetime.withTimeAtStartOfDay.
I'm assuming that you are using the joda time DateTime. If you are, then you can use the following method
def dateTimeAtStartOfHour(s: String) = {
new DateTime(s)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0)
}
val date1 = dateTimeAtStartOfHour("2015-08-03T04:59:00.000")
val date2 = dateTimeAtStartOfHour("2015-08-03T04:15:00.000")
val date3 = dateTimeAtStartOfHour("2015-08-03T04:59:13.000")
Output is
date1: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00
date2: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00
date3: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00
You need to use "truncate" analogue, which is called roundFloorCopy in joda:
https://stackoverflow.com/a/8510936/1349366