Camel Bindy Date format Unmarshalling issue - apache-camel

I ran into an issue with Apache Camel Bindy data format for the Date field parsing from a CSV file.
Date in the CSV is 02/11/2015 03:34:49 PM
Format in the Bindy Class annotated as
#DataField(pos = 8,pattern="MM/dd/yyyy hh:mm:ss a")
private Date time;
Getting below exception
java.lang.IllegalArgumentException: Date provided does not fit the
pattern defined, position: 8, line: 1 at
org.apache.camel.dataformat.bindy.BindyCsvFactory.bind(BindyCsvFactory.java:213)
at
org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat.unmarshal(BindyCsvDataFormat.java:185)
at
org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:67)
It works if the date in the CSV is given as 2/11/2015 03:34:49 PM, with no preceeding 0 in the Month field.
I am using Camel 2.14.1.
Am I doing anything wrong here ?

From the source code of DatePatternFormat.java :
public Date parse(String string) throws Exception {
Date date;
DateFormat df = this.getDateFormat();
ObjectHelper.notNull(this.pattern, "pattern");
// Check length of the string with date pattern
// To avoid to parse a string date : 20090901-10:32:30 when
// the pattern is yyyyMMdd
if (string.length() <= this.pattern.length()) {
// Force the parser to be strict in the syntax of the date to be
// converted
df.setLenient(false);
date = df.parse(string);
return date;
} else {
throw new FormatException("Date provided does not fit the pattern defined");
}
}
It check that parsed string is smaller than the pattern.
Then changing your pattern to MM/dd/yyyy hh:mm:ss aa should fix your problem.

Related

Date/Time Formatting in React

I am working with an API that returns Dates in the format 2022-03-01T11:32:37
Created: {this.props.proposal.OPENWHEN}
Created: 2022-03-01T11:32:37
How do i format this into DD/MM/YYY 24:00:00 ?
Thanks In Advance
Something like the following should be enough:
// Example expected argument: 2022-03-01T11:32:37
function prettifyDateTime (str) {
// Splitting the string between date and time
const [date, time] = str.split("T");
// Assuming 03 is the month and 01 is the day – otherwise, those could be swapped
const [year, month, day] = date.split("-")
// Added slashes and the space before the time
return `${day}/${month}/${year} ${time}`
}
prettifyDateTime("2022-03-01T11:32:37") // '01/03/2022 11:32:37'
Otherwise, I recommend using a date library like date-fns, dayJS, or moment.
You can use the Date object to accomplish what you want. It'll also accept other date formats. Here is one place to find Date description. You also get the benefit of rejecting invalid date formats
function prettifyDateTime(str) {
let date = new Date(str);
// There's other ways of formatting this return
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
}

how to update datetime colum (mssql) with formatted current datetime in querydsl

I want to update datetime column with current datetime.
but I've got an error at QAa.aaa.visitDt line.
The data type of QAa.aaa.visitDt is DateTimePath<java.time.LocalDateTime>
```
Date date = new Date();
String updDt = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
Long ret = queryFactory.update(QAa.aaa)
.set(QAa.aaa.visitCnt, QAa.aaa.visitCnt.add(1))
.set(QAa.aaa.visitDt, Expressions.dateTimePath(LocalDateTime.class, updDt))
.where(QAa.aaa.aNo.eq(aNo))
.execute();
Error Message is below.
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ~
column 90 [update ~~
set aaa.visitCnt =aaa.visitCnt + ?1, aaa.visitDt= 2022-09-12 13:24:36.330
where aaa.aNo = ?3

How to check if a string date has passed a certain date

I have these strings:
yy = "19";
mm = "05";
dd = "31";
These represent the creation date of a certain object in my project. This object expires after one month. How do I check if the object has expired already?
(I came across this solution but thought there might be another way to do it.)
UPDATE: the string date apparently represent the actual expiry date
I ended up using the date format "yymmdd" so I can convert it to type long and do a simple number comparison.
sprintf(buffer, "%s%s%s", yy, mm, dd);
expiryDate = atol(buffer);
// get current date of format "yymmdd" as well
// getCurrentDate() is my function that gets the date from my SDK
currentDate = getCurrentDate();
if(expiryDate >= currentDate)
{
// expired object!
}

Epoch 0 to Date conversion using momnet.js

I need to convert my epoch date to a Date object. I did this using the following code.
let abc = moment(maintenance.actualEndDate * 1000).format('DD/MMM/YYYY hh:mm');
but there is a high chance to 0 as the value for 'maintenance.actualEndDate'.
in this case the transalated date is showing the value as '01/01/1970 12:00'.
I actually need as an empty string in variable abc if the maintenance.actualEndDate is 0
I'm working on angular 4, is there any optimal solution to this?
If what you need is converting non-zero timestamps to a formatted date, and zero timestamps as empty string, a dedicated function would be nice:
function formatTimestamp(secondsSinceEpoch) {
return secondsSinceEpoch===0 ? '' : moment.unix(secondsSinceEpoch).format('DD/MMM/YYYY HH:mm');
}
//...
let abc = formatTimestamp(maintenance.actualEndDate);
(But this has nothing specific to angular)

NancyFX not serializing dates with a trailing Z to indicate UTC/Zulu

We store all of our dates in our database as UTC.
When they are returned to us from the API, they are in the following format
"createdDate":"2014-07-30T18:34:45"
But as you can see, the date doesn't have the trailing Z (which indicates to our Angular app that it's UTC / Zulu). It should look like this
"createdDate":"2014-07-30T18:34:45Z"
I do have the following setting in our Bootstrapper
JsonSettings.ISO8601DateFormat = true;
Where in my config can I ensure that there's a trailing Z for the purpose of UTC parsing?
What version of NancyFx are you using? Because in v0.23.0 or later, the JsonSerializer code has been changed to use the "o" date format instead of the "s" date format, which should give you the trailing Z that you're looking for. (But only on UTC datetimes.)
This is the commit that made this change. Note how DateTimeKind.Unspecified dates are treated as local; that might be one possible cause of your problem, if you're not explicitly creating your DateTime objects as DateTimeKind.Utc.
Below is the NancyFx code that serializes DateTime values, as it looks as of v0.23.0 (after that commit). From https://github.com/NancyFx/Nancy/blob/v0.23.0/src/Nancy/Json/JsonSerializer.cs, lines 480-518:
void WriteValue (StringBuilder output, DateTime value)
{
if (this.iso8601DateFormat)
{
if (value.Kind == DateTimeKind.Unspecified)
{
// To avoid confusion, treat "Unspecified" datetimes as Local -- just like the WCF datetime format does as well.
value = new DateTime(value.Ticks, DateTimeKind.Local);
}
StringBuilderExtensions.AppendCount(output, maxJsonLength, string.Concat("\"", value.ToString("o", CultureInfo.InvariantCulture), "\""));
}
else
{
DateTime time = value.ToUniversalTime();
string suffix = "";
if (value.Kind != DateTimeKind.Utc)
{
TimeSpan localTZOffset;
if (value >= time)
{
localTZOffset = value - time;
suffix = "+";
}
else
{
localTZOffset = time - value;
suffix = "-";
}
suffix += localTZOffset.ToString("hhmm");
}
if (time < MinimumJavaScriptDate)
time = MinimumJavaScriptDate;
long ticks = (time.Ticks - InitialJavaScriptDateTicks)/(long)10000;
StringBuilderExtensions.AppendCount(output, maxJsonLength, "\"\\/Date(" + ticks + suffix + ")\\/\"");
}
}
As you can see, requesting ISO 8601 date format will get you the 2014-07-30T18:34:45 format rather than the number of milliseconds since the epoch, but it will assume local times if the value being serialized has a Kind equal to DateTimeKind.Local.
So I have two suggestions for you: upgrade to v0.23 of NancyFx if you're still on v0.22 or earlier (v0.22 used the "s" date format, which does not include timezone info, for serializing DateTime values). And if the DateTime objects you're serializing aren't explicitly set to DateTimeKind.Utc, then make sure you specify Utc (since the default is Unspecified, which NancyFx treats as equivalent to Local).

Resources