How to split Japanese Date which include month and year into or Date format [duplicate] - date

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

Related

Invalid date format [Flutter] [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
Closed 9 months ago.
I've been trying convert a String to DateTime in dart using DateTime.parse(dateStart), where 'dateStart' is my variable that I use to get the date, for example in the next format '24/12/2022', but I catch invalid date format, I would like to known which are the validates formats to use in dart.
DateTime.parse(dateStart)
You need to use intl package to parse string date to datetime object. Please check sample Datetime parse example as below.
DateTime tempDate = new DateFormat("dd/MM/yyyy").parse(savedDateString);
You can format '24/12/2022' to DateTime like this:
final dateTime = DateFormat('dd/MM/yyyy').parse('24/12/2022');

What type of date string is this? `20210713T015433.354Z` [duplicate]

This question already has answers here:
What is this date format? 2011-08-12T20:17:46.384Z
(11 answers)
Closed 1 year ago.
I'm working with some data where I don't know how the time string was generated but an example of what the date string looks like is this 20210713T015433.354Z
I've looked at some examples of date strings (ex1 and ex2) but haven't had any luck decoding it.
I know that the first half of the string is YYYYMMDD, and then I think that T means times but then I don't know what 015433.354Z is.
My eventual goal is to make this into a Date objects in JS but I can't deduce the formatting. beyond the day it occured.
The date format provided is ISO 8601. Maybe you want to try the following to get a Date object:
new Date(Date.UTC(2021, 7, 13, 1, 54, 33, 354))
As pointed in this SO answer.

Scala Last Year

I haven't been able to find an answer for this yet for Scala. I am trying to build a variable in scala for last year and last 2 months, and return in YYYY-MM-dd format. Here is what I have built out. Right now instead of the variable returning the expected date formatted the way im requesting the variable just returns the expression. Any thoughts on how I can change my code to present the variable last_year with the last year in a string format?
val last_year=date_sub(current_date()),365)
val last_2=date_sub(current_date()),60)
desired result: last_year= "2021-03-08"
last_2= "2021-01-08"
Here's the same result using the newer java.time library in place of the old and outdated Calendar and SimpleDate options.
val prev_yr = java.time.LocalDate.now().minusYears(1L).toString
Think I have the answer, just in case anyone needs this later on:
val cal:Calendar=Canlendar.getInstance()
val format=new SimpleDateFormat("YYYY-MM-dd")
cal.add(Calendar.Date,-365)
val prev_yr:String=format.format(cal.getTime)
println(prev_yr)

Exact 1 year previous date in Scala

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

Convert a date in string YYYY-MM-DD to MM/DD in JSTL [duplicate]

This question already has answers here:
Convert and format a Date in JSP
(6 answers)
Closed 5 years ago.
I have a string field that is shown as a date in YYYY-MM-DD format. I am trying to convert this field on my JSP to MM/DD date format. Here is the JSTL code I have now:
<fmt:parseDate var="taxFromDate" value="${a8spt214.fromDate}" pattern="MM-dd"/>
<fmt:parseDate var="taxToDate" value="${a8spt214.toDate}" pattern="MM-dd"/>
When I try running this I get a compile error saying the following
In <parseDate>, value attribute can not be parsed: "2017-06-30"] with root cause java.text.ParseException: Unparseable date: "2017-06-30"
is it possible to convert a string to a date filed in JSP without actually using the year in the format? Or is the pattern invalid?
Thanks
You can use this Code to show this type of date MM/DD/YY
<p>Formatted Date : <fmt:formatDate type = "both"
dateStyle = "short" timeStyle = "short" value = "${now}" /></p>
The Code above will Print this result:
Formatted Date : 23/09/10 14:27
I hope to help you solve problem.