I have a date string like this - 190515(YYMMDD) format. How to get a populate a exact one year previous date from this? ie, Expected Answer - 180515 (YYMMDD). I am using Scala for this.
I have tried the below. But am getting the below exception - Text '190515' could not be parsed
import java.time.LocalDateTime
import java.time.format.{ DateTimeFormatter, DateTimeParseException }
val d1 =LocalDateTime.parse("190515",DateTimeFormatter.ofPattern("yyMMdd"))
d1.minusYears(1)
LocalDateTime is used for points of time having both a date and a time precision. Yours only have a date, so you should be using LocalDate
Related
The code is as follows:
DateTime time = DateFormat("hh:mm").parse(doc[i]["Time"]);
The doc[i]["Time"] value is supposed to be a military time like 19:42. I followed the guidelines about using DateFormat but the end result variable time contains "1970-01-01 19:42:00.000". Is there a way for time to just contain "19.42"?
To get time in 24hrs format change your code to
String time = DateFormat("HH:mm").format(doc[i]["Time"]);
H -> is for 24 hours format
It is not possible to use DateTime to show a specific format, must always become a String.
A DateTime type will always store everything including year, month, hour, minutes, seconds, milliseconds, etc.
But when you later use the variable, you can chose what to display.
For example, you can chose to display just the military time. Here is one example using the Intl package (here)
Text(DateFormat('Hm').format(yourVariable))
Don't forget to import
import 'package:intl/intl.dart';
This question already has answers here:
How does Java "week year" really work?
(2 answers)
Why I get a wrong result when parsing a date from string with SimpleDateFormat ? (Java) [duplicate]
(1 answer)
java parsing string to date
(2 answers)
Is SimpleDateFormat in Java work incorrect or I did any mistake? See code sample [duplicate]
(2 answers)
Closed 2 years ago.
I want to split Japanese date which is in format "2020年7月" into separate array so that can get the year and month from that.
I tried SimpleDateFormatter to format Japanese date with and have the year and date separated with "/"but the output is wrong:
Tried this code:
val inputFormat = SimpleDateFormat("YYYY年M月")
val date = inputFormat.parse("2020年7月")
val outputText = SimpleDateFormat("YYYY/MM").format(date)
input :2020年7月
output:2020/12
Can anyone help me in this?
You should be using java.time for this because the datetime classes from java.util are outdated (but still not deprecated).
The following example shows a way using a java.time.format.DateTimeFormatter for parsing a String formatted according to a certain pattern and a java.time.YearMonth, a class obviously designed for the purpose of creating a month in a year without respect to a day of month:
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
fun main() {
val jpYearMonth = "2020年7月"
val japaneseDtf = DateTimeFormatter.ofPattern("uuuu年M月")
val yearMonth = YearMonth.parse(jpYearMonth, japaneseDtf)
println(yearMonth.format(DateTimeFormatter.ofPattern("uuuu/MM")))
}
In order to achieve your desired output this code has to use a different DateTimeFormatter defining the output pattern because the default pattern (internal use when you just println(yearMonth)) would be hyphon-separated (2020-07):
2020/07
The question is kind of similar with the problem: Change the timestamp to UTC format in Pyspark
Basically, it is convert timestamp string format ISO8601 with offset to UTC timestamp string(2017-08-01T14:30:00+05:30 -> 2017-08-01T09:00:00+00:00 ) using scala.
I am kind of new to scala/java, I checked spark library which they dont have a way to convert without knowing the timezone, which I dont have a idea of timezone unless (I parse it in ugly way or using java/scala lib?) Can someone help?
UPDATE: The better way to do this: setup timezone session in spark, and use df.cast(DataTypes.TimestampType) to do the timezone shift
org.apache.spark.sql.functions.to_utc_timestamp:
def to_utc_timestamp(ts: Column, tz: String): Column
Given a timestamp like '2017-07-14 02:40:00.0', interprets it as a time in the given time zone, and renders that time as a timestamp in UTC. For example, 'GMT+1' would yield '2017-07-14 01:40:00.0'.
You can use the java.time primitives to parse and convert your timestamp.
scala> import java.time.{OffsetDateTime, ZoneOffset}
import java.time.{OffsetDateTime, ZoneOffset}
scala> val datetime = "2017-08-01T14:30:00+05:30"
datetime: String = 2017-08-01T14:30:00+05:30
scala> OffsetDateTime.parse(datetime).withOffsetSameInstant(ZoneOffset.UTC)
res44: java.time.OffsetDateTime = 2017-08-01T09:00Z
I read from a very old post here on stackoverflow that joda is a possible solution to convert Unix timestamp.
import org.joda.time._
new DateTime(1511544070).toString("yyyy-MM-dd")
I got 1970-01-18 for this case, however, this is wrong because the date should be
according to this online converter: 11/24/2017 # 5:21pm (UTC)
It is possible the online converter is correct because the sample unix timestamp 1511544070 is from a dataset that date range is November 25 to December 03, 2017, the dataset is from China time which is 8 hours ahead of UTC, meaning 11/24/2017 # 5:21pm (UTC) is actually 11/25/2017 # 1:21am (Beijing Time)
Where can I get a working library or is there a working library that can get the same result like the online converter?
You can do that using java.time:
import java.time.{ LocalDateTime, ZoneOffset }
import java.time.format.DateTimeFormatter
LocalDateTime.ofEpochSecond(1511544070, 0, ZoneOffset.UTC)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd # h:mm a"))
Looking at the documentation for joda-time we see that a DateTime can take a Long specifying the milliseconds since 1 Jan 1970. However, you seem to be providing a value in seconds. Joda-time is actually calculating it correctly, since since 1511544070/(1000*3600*24) equals 17.49 days, i.e. 1970-01-18.
To get the expected result multiply with 1000:
new DateTime(1511544070*1000).toString("yyyy-MM-dd")
To get the time in another timezone, add withZone() as follows (for Shanghai/Beijing):
new DateTime(1511544070*1000).toString("yyyy-MM-dd")
.withZone(DateTimeZone.forID("Asia/Shanghai"))
I am new to Python.I would like to know how I can manipulate a string that contains the date. For example, I want to transform today's date to the first day of the next year.
For example:
07/02/2018 --> 01/01/2019
How is that possible in Python 2.7?
You can use datetime package from python. Convert your string in to a date and do the manipulations. See more at here.
https://docs.python.org/2/library/datetime.html