-3

I have a requirement like, the date difference should be calculated from an input date and the current date in Talend java platform. In that case I can use TalendDate.diffDate(date1, date2) : long method, where date1 and date2 are of type Date

So I can invoke that method inside my own method,

public static long findDateDiff(Date date2){ 
   Date date1 = new Date(); //current date of format Fri Feb 23 09:37:33 IST 2018
   // date2 format is 'dd/MM/yyyy'
   long diff = TalendDate.diffDate(date1,date2);
   return diff
}

Here, the parameter date2 is of type Date and format 'dd/MM/yyyy'. But when I get the current date, it is of different format, so I have a trouble in passing the dates with different formats.

How can I convert the current date format to 'dd/MM/yyyy' into the same variable date2?

Date date1 = new Date(); //current date of format Fri Feb 23 09:37:33 IST 2018 String sDate = new SimpleDateFormat.format(date1); //returns only String format

If I use the above method SimpleDateFormat.format(Date), it returns a String value, and how can I pass the value to diffDate(date1, date2) where both the parameter types are Date?

So I just need to convert the current date, into a specified format and store the result into the same Date variable, rather than getting it as a String.

Please help me resolve this issue. Thanks in advance.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Sekar
  • 11
  • 3
  • 2
    *"Here, the parameter date2 is of type Date and format 'dd/MM/yyyy'"* - `Date` does not have a internal concept of format, it just a container for the number of seconds from a given point in time. – MadProgrammer Feb 23 '18 at 04:46
  • Are you looking for date difference in term of number of days/hours/minutes/seconds/milliseconds? – Balwinder Singh Feb 23 '18 at 04:47
  • @Balwinder Singh. yes the date different in number of days.. – Sekar Feb 23 '18 at 04:48
  • 1
    You don’t have a problem. `TalendDate.diffDate` will work with the `Date` objects you already have. This is because, as @MadProgrammer says, a `Date` object cannot have a format in it anyway. So don’t worry. – Ole V.V. Feb 23 '18 at 05:44
  • Questions about date formats and about number of days between two dates have been asked (I believe) thousands of times. Please search before asking and find a good answer faster than anyone can write one here. – Ole V.V. Feb 23 '18 at 05:46

1 Answers1

0

This is one of the topics with Talend which come up for a number of reasons again and again.

As stated before, an actual Date holds only a numerical value, the formatting happens differently and is not connected to the date value. You are using a date function in Talend, just make sure both values are actual dates (parse a String if you need to). It will help a lot to take the correct data type.

Since this happens so often, I created an article about this topic.

tobi6
  • 8,033
  • 6
  • 26
  • 41