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
Related
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"
I am trying to extract dates between two dates.
If my input is:
start date: 2020_04_02
end date: 2020_06_02
Output should be:
List("2020_04_02","2020_04_03", "2020_04_04", "2020_04_05", "2020_04_06")
So far i have tried:
val beginDate = LocalDate.parse(startDate, formatted)
val lastDate = LocalDate.parse(endDate, formatted)
beginDate.datesUntil(lastDate.plusDays(1))
.iterator()
.asScala
.map(date => formatter.format(date))
.toList
import java.time.format.DateTimeFormatter
private def formatter = DateTimeFormatter.ofPattern("yyyy_MM_dd")
But i think it could be done even in a more refined way
I'm not super proud of this, but I've done it this way before:
import java.time.LocalDate
val start = LocalDate.of(2020,1,1).toEpochDay
val end = LocalDate.of(2020,12,31).toEpochDay
val dates = (start to end).map(LocalDate.ofEpochDay(_).toString).toArray
You end up with:
dates: Array[String] = Array(2020-01-01, 2020-01-02, ..., 2020-12-31)
What you are doing is correct but your end date is not matching what you are expecting:
import java.time.format.DateTimeFormatter
import java.time.LocalDate
import scala.jdk.CollectionConverters._
private def formatter = DateTimeFormatter.ofPattern("yyyy_MM_dd")
val startDate = "2020_04_02"
val endDate1 = "2020_04_06" // "2020_06_02"
val endDate2 = "2020_06_02"
val beginDate = LocalDate.parse(startDate, formatter)
val lastDate1 = LocalDate.parse(endDate1, formatter)
val lastDate2 = LocalDate.parse(endDate2, formatter)
val res1 = beginDate.datesUntil(lastDate1.plusDays(1)).iterator().asScala.map(date => formatter.format(date)).toList
val res2 = beginDate.datesUntil(lastDate2.plusDays(1)).iterator().asScala.map(date => formatter.format(date)).toList
println(res1) // List(2020_04_02, 2020_04_03, 2020_04_04, 2020_04_05, 2020_04_06)
println(res2) // List(2020_04_02, 2020_04_03, 2020_04_04, 2020_04_05, 2020_04_06 ... 2020_05_31, 2020_06_01, 2020_06_02)
This function will return List[String].
import java.time.format.DateTimeFormatter
import java.time.LocalDate
val dt1 = "2020_04_02"
val dt2 = "2020_04_06"
def DatesBetween(startDate: String,endDate: String) : List[String] = {
def ConvertToFormat = DateTimeFormatter.ofPattern("yyyy_MM_dd")
val sdate = LocalDate.parse(startDate,ConvertToFormat)
val edate = LocalDate.parse(endDate,ConvertToFormat)
val DateRange = sdate.toEpochDay.until(edate.plusDays(1).toEpochDay).map(LocalDate.ofEpochDay).toList
val ListofDateRange = DateRange.map(date => ConvertToFormat.format(date)).toList
ListofDateRange
}
println(DatesBetween(dt1,dt2))
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)
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
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))