Interesting DATE question -- how to get ?th day of the year?

Seven Stars

Interesting DATE question -- how to get ?th day of the year?

I have an interesting requirement: to get the ?th of day for a current date.

Let me show something real quick for easy understanding:

001- source files.JPG  

We would get a file from vendor every day, and it is listed as ?th day of the year.

The file from Jan 1st is named .2019001, the file from Feb 1st is names as .2019032, ...

and the file from Dec 31th would be .2019365. The ?th day part is always 3 digits.

 

In order to fetch the correct file for process each day, I will create a global variable "runDate" and use 

it as the filemask to find the file. But I was not able to create ?th day as I wanted.

Here is the code I tried to get ?th day, neither of them works.

 

002 - java1.JPG  

 

003 - java2.JPG

 

Date_run.JPG 

 

As you can see if the result console. For Feb 1st, runDate1 = 201932,  runDate2 = 2019001 (runDate2 will always be reset to 001 for the 1st day of a month), what I'm expecting is runDate = 2019032.

 

I am wondering if there is an easy solution to generate the correct ?th day ( 3 digits) which meets my need. 

Thanks!

 

 


Accepted Solutions
Thirteen Stars

Re: Interesting DATE question -- how to get ?th day of the year?

Hi,

 

try this:

 

context.dayOfYear = 
(StringHandling.LEN(TalendDate.formatDate("D",TalendDate.getCurrentDate()))<3?
TalendDate.formatDate("yyyy",TalendDate.getCurrentDate())+StringHandling.STR('0',3 - StringHandling.LEN(TalendDate.formatDate("D",TalendDate.getCurrentDate())))+TalendDate.formatDate("D",TalendDate.getCurrentDate())
:TalendDate.formatDate("yyyy",TalendDate.getCurrentDate())+TalendDate.formatDate("D",TalendDate.getCurrentDate()) );
System.out.println(context.dayOfYear);
-----------
Nine Stars

Re: Interesting DATE question -- how to get ?th day of the year?

same as above without ? operator

Same thing without ? operatory

TalendDate.formatDate("yyyy",TalendDate.getCurrentDate())
+
String.format("%03d",Integer.parseInt(TalendDate.formatDate("D",TalendDate.getCurrentDate())))
Regards
Abhishek KUMAR
( Please mark the post as resolved if you found your solution )
Seven Stars

Re: Interesting DATE question -- how to get ?th day of the year?

Thanks you both for your solutions!

String.format("%03d",Integer.parseInt(TalendDate.formatDate("D",TalendDate.getCurrentDate())))

 

Could you please explain to me what is "%03d" doing here? I know it would format the date to be ?th

(3 digits) of the year, but I want to know what is each sign/number/letter doing exactly.

 

Thanks!

TM

 

 

 

 


All Replies
Thirteen Stars

Re: Interesting DATE question -- how to get ?th day of the year?

Hi,

 

try this:

 

context.dayOfYear = 
(StringHandling.LEN(TalendDate.formatDate("D",TalendDate.getCurrentDate()))<3?
TalendDate.formatDate("yyyy",TalendDate.getCurrentDate())+StringHandling.STR('0',3 - StringHandling.LEN(TalendDate.formatDate("D",TalendDate.getCurrentDate())))+TalendDate.formatDate("D",TalendDate.getCurrentDate())
:TalendDate.formatDate("yyyy",TalendDate.getCurrentDate())+TalendDate.formatDate("D",TalendDate.getCurrentDate()) );
System.out.println(context.dayOfYear);
-----------
Nine Stars

Re: Interesting DATE question -- how to get ?th day of the year?

same as above without ? operator

Same thing without ? operatory

TalendDate.formatDate("yyyy",TalendDate.getCurrentDate())
+
String.format("%03d",Integer.parseInt(TalendDate.formatDate("D",TalendDate.getCurrentDate())))
Regards
Abhishek KUMAR
( Please mark the post as resolved if you found your solution )
Thirteen Stars

Re: Interesting DATE question -- how to get ?th day of the year?


@akumar2301 wrote:
Same thing without ? operatory

TalendDate.formatDate("yyyy",TalendDate.getCurrentDate())
+
String.format("%03d",Integer.parseInt(TalendDate.formatDate("D",TalendDate.getCurrentDate())))

yes, good variant!

-----------
Seven Stars

Re: Interesting DATE question -- how to get ?th day of the year?

Thanks you both for your solutions!

String.format("%03d",Integer.parseInt(TalendDate.formatDate("D",TalendDate.getCurrentDate())))

 

Could you please explain to me what is "%03d" doing here? I know it would format the date to be ?th

(3 digits) of the year, but I want to know what is each sign/number/letter doing exactly.

 

Thanks!

TM

 

 

 

 

Nine Stars

Re: Interesting DATE question -- how to get ?th day of the year?

It say that String formatting of number with fixed width of 3 and 0 left padded.
If you want know more below is quick link
https://dzone.com/articles/java-string-format-examples

Please set the as resolved if all OK.

Regards
Abhishek KUMAR
( Please mark the post as resolved if you found your solution )
Seven Stars

Re: Interesting DATE question -- how to get ?th day of the year?

Thanks guys!

Cloud Free Trial

Try Talend Cloud free for 30 days.

Tutorial

Introduction to Talend Open Studio for Data Integration.

Definitive Guide to Data Integration

Practical steps to developing your data integration strategy.

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.