I tried parsing a string in a namedQuery, but it seems doesnt work. I have this code in my domain class:
searchBirthdaten{ q ->
def dates = Date.parse("yyyyy:MM:dd HH:mm:ss", "2011-9-21 00:00:00")
eq 'birthDate' , dates)
}
But I always got this error:
Unparseable date: "2011-9-21 00:00:00"
I really dont understand why this is happening. Any idea?
Your date input string has to be in the format you defined: yyyy:MM:dd HH:mm:ss (corrected)
So your 3 issues were:
You are using the "-" character to delimit you date for parsing but your format string is using ":"
You have 5 ys in your format string i.e. yyyyy:MM.... Which won't be valid for another 8 thousandish years ;)
You define your month format as MM but you are passing only '9', this will need to be '09' to match your fomat string.
Related
I am trying to change the date format in txmlmap component but its not working
i want change date format
from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
expected output:- yyyy-mm-dd HH:mm:ss
You can parse your string to a date using your source pattern and then format that date to a string using your target pattern:
TalendDate.formatDate("yyyy-mm-dd HH:mm:ss", TalendDate.parseDate("yyyy-MM-dd'T'HH:mm:ss.SSSz", myDateString))
In almost all coding languages format is text, while date is a double. That means you must first make a date of the first expression, before setting the new format of that date. But in Your case the 'T' is some kind of special format that need to be replaced with a blanck space. I have no idea about what it would look like in talend but in VB it would look like this:
' from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
DateTxt = "2022-12-01'T'22:45:10"
DateTxt = Replace(DateTxt, "'T'", " ")
MyDate = CDate(DateTxt)
MsgBox Format(MyDate, "yyyy-mm-dd HH:mm:ss")
Let's say I have this Model:
type alias Model =
{ currentDate : String
, yesterdayDate : String
}
The CurrentDate I got from Html input type date (Date Picker) is in format YYYY-MM-DD
Html Form
input [ name "date", type_ "date", onInput UpdateDate ] []
Update.elm
UpdateDate date ->
let
-- Get Yesterday Date function here
in
( { model | currentDate = date, yesterdayDate = "" }, Cmd.none )
In this situation , how can i get yesterday Date in String ?
My idea is parse the day into INT and using subtraction method to get Yesterday day but I cannot find any way to do it... Any help is appreciate.
Convert the string date to Posix, convert the Posix to milliseconds since epoch, subtract the amount of milliseconds in a day, convert the resulting milliseconds back to Posix and the Posix to an ISO8601 string. Take the first 10 characters from that string.
module Main exposing (main)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
import Iso8601
import Time exposing (Posix)
sampleDate =
"2020-05-01"
subtractDays : Int -> Posix -> Posix
subtractDays days time =
(Time.posixToMillis time - (days * 24 * 60 * 60 * 1000))
|> Time.millisToPosix
subtractDaysFromIsoDate : Int -> String -> String
subtractDaysFromIsoDate days date =
Iso8601.toTime date
|> Result.map (subtractDays days >> Iso8601.fromTime >> String.left 10)
|> Result.withDefault date
main =
text <| subtractDaysFromIsoDate 1 sampleDate
Note that in this implementation if the string is not a valid date it will just be returned unmodified rather than fail. You might want to capture that this operation can fail.
As you can trust that you get a valid string format from html and are aware of the date package, you can split the date string into 3 strings, convert each into an integer and then construct today and yesterday as a Date value.
Questions you should ask yourself:
Do you really want to store the date as a String? The Date type might be more useful if you want to do something else then just display the string value.
And do you really want to store both today and yesterday? The latter can be easily computed when needed.
Example for string splitting:
case
String.split "-" date
|> List.map String.toInt
of
[ Just year, Just monthInt, Just day ] ->
-- convert monthInt to `Month`
-- construct current date
-- add -1 `Day`
Debug.todo "todo" 2
_ ->
Debug.todo "invalid date format" date
I combine two columns; date and time. When I pass the date and time hot coded it works fine but when I pass it through a column it throws the error:
Unparseable date: "05/05/1992"
I already tried this:
MaterialCodeCSV.xdate == null ?
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss")) :
TalendDate.parseDateLocale("yyyy/mm/dd HH:mm:ss",MaterialCodeCSV.xdate.toString() + MaterialCodeCSV.xtime.toString(),"EN");
Java code in Talend:
Date handling can be a bit tricky if using wrong data types. I assume you want to fill a field which is a Date. There are several errors with this way:
MaterialCodeCSV.xdate == null ?
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss")) :
TalendDate.parseDateLocale("yyyy/mm/dd H:mm:ss",MaterialCodeCSV.xdate.toString()+ MaterialCodeCSV.xtime.toString(),"EN");
If MaterialCodeCSV.xdate == null you create a date and parse it again instantly? That seems unneccessary complex and inefficient. Change this to TalendDate.getCurrentDate()
Then if xdate is not null, you just concat xdate and xtime, use toString() and try to parse this. Again, this seems unneccesary complex. If I just assume now and xdate and xtime are already Date fields, you could write it as this: MaterialCodeCSV.xdate + MaterialCodeCSV.xtime.
If both are String fields, you have to make sure that xdate is formatted yyyy/MM/dd and xtime is HH:mm:ss. Then you could exclude .toString()
Also, if both are String fields, you have to add an additional space: MaterialCodeCSV.xdate + ' ' + MaterialCodeCSV.xtime
Additionally, in the first case you parse with yyyy-MM-dd HH:mm:ss. In the second case you parse with yyyy/mm/dd H:mm:ss. This reads "year/minute/day". Also there is only one hour digit, not allowing anything after 9:59:59 o'clock to be parsed. Correctly you should use yyyy/MM/dd HH:mm:ss.
So to conclude it should look like this (if I assume correctly and you are using correctly formatted String fields for xdate and xtime):
MaterialCodeCSV.xdate == null ?
TalendDate.getCurrentDate() :
TalendDate.parseDateLocale("yyyy/MM/dd HH:mm:ss", MaterialCodeCSV.xdate + ' ' + MaterialCodeCSV.xtime,"EN");
I´m trying to pase the next String using LocalDateTime, but I always get de unparsed text found error:
Error java.time.format.DateTimeParseException: Text '2016-08-18 14:27:15.103+02' could not be parsed, unparsed text found at index 10
Here is my String: convertDate: '2016-08-18 14:27:15.103+02'
And my code:
public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException {
LocalDate dateTime =LocalDate.parse(convertDate);
return dateTime;
}
I guess is not too complicated, buy I´m not able to see the error. Could the +02 in the String be the cause?
tl;dr
OffsetDateTime odt = OffsetDateTime.parse ( "2016-08-18 14:27:15.103+02" , DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" ) ) ;
Details
The Answer by greg-449 is correct about the problem (using a date-only object for a date-time value) but not the solution.
That Answer uses LocalDateTime which unnecessarily throws away valuable information about the offset-from-UTC. A LocalDateTime does not represent a specific moment on the timeline, only a vague idea about possible moments depending on adjusting into a particular time zone.
The +02 is an offset-from-UTC meaning “two hours ahead of UTC”. So in UTC the time-of-day for this simultaneous moment is 12 hours, 2 hours less than your 14 hours. This does represent a specific moment on the timeline. This offset is the valuable information you are throwing away with a LocalDateTime rather than an OffsetDateTime.
The format of your string is in SQL format, which is close to standard ISO 8601 format. Merely replace the SPACE in the middle with a T. The java.time classes use ISO 8601 formats by default, so no need to specify a formatting pattern.
String input = "2016-08-18 14:27:15.103+02";
String inputModified = input.replace ( " " , "T" );
Unfortunately, Java 8 has a bug in parsing offset values abbreviated to just an hour or offset values omitting the colon between hours and minutes. Fixed in Java 9. But in Java 8, we need to adjust the input.
// Workaround for Java 8 where 2-digit offset fails parsing. Fixed in Java 9.
int lengthOfAbbreviatedOffset = 3;
if ( inputModified.indexOf ( "+" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
// If third character from end is a PLUS SIGN, append ':00'.
inputModified = inputModified + ":00";
}
if ( inputModified.indexOf ( "-" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
// If third character from end is a PLUS SIGN, append ':00'.
inputModified = inputModified + ":00";
}
Now parse.
OffsetDateTime odt = OffsetDateTime.parse ( inputModified );
Dump to console. Note how we transformed +02 into +02:00.
System.out.println ( "input: " + input + " | inputModified: " + inputModified + " | odt: " + odt );
input: 2016-08-18 14:27:15.103+02 | inputModified: 2016-08-18T14:27:15.103+02:00 | odt: 2016-08-18T14:27:15.103+02:00
Alternatively, specify a formatting pattern. The offset-parsing bug does not bite when using this formatting pattern.
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" );
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
Database
Coming from Postgres, you should be retrieving the value as a date-time object rather than a String.
If your JDBC driver complies with JDBC 4.2 you can call ResultSet::getObject to get an Instant or OffsetDateTime. If not, call ResultSet::getTimestamp to get a java.sql.Timestamp, then immediately convert to java.time by calling toInstant on the Timestamp object.
Stick with java.time for your business logic; use the java.sql types briefly and only for exchange with the database.
Your code is using LocalDate which only parses a date - not a date and time so you are getting an error when the parse finds the space after the date.
So you should be using LocalDateTime but LocalDateTime.parse(String) expects an ISO format date which is not the format you are using.
So you need to use a DateTimeFormatter to specify the format of your input string. Something like:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");
LocalDateTime result = LocalDateTime.parse(convertDate, format);
I have a table with a YearMonth column (201302) in it.
I created YearMonth a parameter.
Now I would like to write an expression in SSRS so that every time the report runs the date and month would look like this Feb-2013.
Could you please suggest me why following expression did not work.
Thanks
=CStr(Right(MonthName(Month(Parameters!NLR_YearMonth.Value)),3))
+ "-" +
Left(Year(Parameters!NLR_YearMonth.Value),4)
Try This:
=Format(DateValue(MonthName(Right(Parameters!NLR_YearMonth.Value, 2))
+ "," +
Left(Parameters!NLR_YearMonth.Value,4)), "Y")
The expression goes as follows:
Cutting the string to get the month.
Converting the mount to MountName
Creating a comma seperator between the month and the year
Cutting the string to get the year
Converting the returns string to Date object using the DateValue function
Formatting the Date to fits your needs
The results should be:
February, 2013