Talend timestamp - Getting a zero value instead of timestamp - java

I am converting string to date using a tConvertType component in Talend.The data(source is in String) is getting loaded when I do the date pattern yyyy-mm-dd but when I try with timestamp yyyy-MM-dd HH:mm:ss then I am getting an error. The data from the source has the timestamp but then I am getting an error.
For Eg : I have the source as 2015-09-03 14:14:90 , since data is in string so used tconverttype and then for destination the data type is date. But if I use timestamp then I am getting an error of Unparseable date and if I change to yyyy-mm-dd then the data is coming as 2015-09-03 00:00:00 which is wrong

try
convert(timestamp,expression)
i had the same problem in the past and convert fixed that

Using the type date and date format "yyyy-MM-dd' 'HH:mm:ss" it works fine
I recommand you to use date format by pressing Ctrl+space and choose from the list.

Related

how to get the date in the format that is displayed in sql table?

I have an oracle database table with a date column. when I query the table date is displayed in dd-MMM-yy format. For example 17-OCT-19 format. But when I query the table from java and print the value I am seeing the date in yyyy-MM-dd format. Is there a way where I can fetch the date the same as that is displayed in dd-MMM-yy format? I have no idea why the date is getting displayed in a different format in java.
DateFormat activityDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date activityDate = activityDateFormat.parse();
activityDateFormat = new SimpleDateFormat("dd-MMM-yy");
finalActivityDate = activityDateFormat.format(activityDate);
I am using the piece of code to convert the date from yyyy-MM-dd to dd-MMM-yy format. The NLS settings for Date format in the database are DD-MON-RR. Is there any reason why I don't get the date the same as DD-MON-RR? can someone please help me?
Select the date column as string and format it dd-mmm-yy from the SQL query.
The client you are using to query Oracle, SQL Developer is often used, is the one formatting the date.
Java gets the date as a date data type and will use whatever is the default format when converting it to a string

How to convert date in string with or without time into DATE

I have a date in string format like "2017-11-16" or "2017-11-16 12:59:11.243". I have to convert it to Date.
Below is my code:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
Date createdOn = formatter.parse(date);
It works fine when the date is "2017-11-16 12:59:11.243". But not able to format with "2017-11-16" date.
String date is coming from URL, so it could be either with time or without time. How can I convert it into Date in spite of the fact what type of date is coming from URL?
Actually, I have to find row from the database based on the date. In the database, the date is storing in the format like 2017-11-16 12:59:11.243. But If from URL 2017-11-16 is coming then it should find according to this date.
You can modify the pars based on the presence of time,
if(dateOrDataTime.contains(":")){
//Use parser with date-time. & fetch with equal
}else{
//Use parser for date only. & fetch using date(date_time)
}

how to send date through URL in ajax

Hi i have to append Date to a URL, maintaining its Datatype as Date and not as String
When in append Date to URL as "?date=" + $(target).val();
i fetch it through #RequestParameter in my Controller
But the problem here lies that i cant access it as Date but as String.
If i access it as Date it does not gives an error but changes the format of the Date from 2017-05-12 00:00:00 to something like Fri MAY 12 00:00:00 IST 2017.
So i need to send data as date through AJAX so when i access in my Controller i can maintain its format
As far as i know when i fetch the data as Date it gives me internal representation of Date because there is internal conversion of String to Date
Thus i need to know how i can send date through URL in AJAX
You can't pass types via GET URL parameters.
You need to convert the type on your java code.
For example:
new SimpleDateFormat( "MM/dd/yyyy H:m:s" ).parse( #RequestParam( "date" ) )
It's all in the javadoc for SimpleDateFormat

Comparing to get seconds difference between java and mysql datetime

I read a mysql date time field into one string e.g.
String arriveTime = rs1.getString("arriveTime");
Next step I try to get the current date and time using java to be similar format like the one I got from mysql.
DateFormat outDf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateTimer=null;
Date date = Calendar.getInstance().getTime();
currentDateTimer=outDf.format(date);
How can I minus the currentDateTime and arriveTime to get the net results in seconds. I would prefer to do it purely via java
First, I would not read a MySQL date-time into a String. I would change this,
String arriveTime = rs1.getString("arriveTime");
to
java.sql.Date arriveTime = rs1.getDate("arriveTime");
Then you can use basic subtraction to get the result in milliseconds, then divide by a thousand to get that in seconds - so
long diff = new java.util.Date().getTime() - arriveTime.getTime();
System.out.println(diff / 1000);
I read a mysql date time field into one string
If the column is varchar type it is ok you can read it using resultsetObject.getString()
But if your column type is Date then is it always recommended to get the value using resultset.getDate()
Mysql stores date in the format yyyy-MM-dd
I try to get the current date and time using java to be similar format
like the one I got from mysql.
When you do resultset.getDate() it will give you the java.sql.Date in format yyyy-MM-dd

Timestamp getting as long instead of date format

I have a one rest method from Spring3 which returns expected JSON response except date which is in timestamp.
In database date is stored as 2013-08-08 00:30:00. Before sending response to the application, the date I am getting as 2013-08-08 00:30:00. I have checked it in debug mode. But after complete execution of rest-service, I got the date in long format as 1375902000000.
I want to return same timestamp format instead of long format.
I don't want to convert long timestamp to again date format at client side.
By default it is serialized as long if you want to send it like 2013-08-08 00:30:00 you may try asString()
You could do the following to keep your desire date format:
Declare the bean property as String.
Format the date object according to your desire format, example yyyy-dd-MM hh:mm:ss

Categories