Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:51:07+00:00 2026-05-27T10:51:07+00:00

I’m working on a program where I want to output to a file that

  • 0

I’m working on a program where I want to output to a file that has the current data in yyy/MM/dd format appended to the filename.

I want to inject the File object representing the output file location into the class that needs it using Spring.

However, I don’t know how to append the current date to the filename argument when creating the File object.

In actual code it’s easy:

String outputFileName = "someFile";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
outputFileName += " " + sdf.format(new Date());
File outputFile = new File(outputFileName);

How can I do this in my Spring bean configuration file?

Is it even possible to do this, and if so how could I go about it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:51:07+00:00Added an answer on May 27, 2026 at 10:51 am

    Well… technically you can do almost everything. I’m using FastDateFormat because it’s both fast (duh!) and thread-safe. java.text.SimpleDateFormat can be used as well:

    <bean id="fastDateFormat" class="org.apache.commons.lang.time.FastDateFormat" factory-method="getInstance">
        <constructor-arg value="yyyy/MM/dd"/>
    </bean>
    
    <bean id="currentDate" class="java.util.Date" factory-bean="fastDateFormat" factory-method="format">
        <constructor-arg>
            <bean class="java.util.Date"/>
        </constructor-arg>
    </bean>
    

    And then simply inject:

    @Resource
    private String currentDate;  //2011/12/13
    

    Note that it would be much simpler to run it in plain Java or using @Configuration approach:

    @Bean FastDateFormat fastDateFormat() {
      return new FastDateFormat("yyyy/MM/dd");
    }
    
    @Bean String currentDate() = {
      return fastDateFormat().format(new Date());
    }
    

    That being said, why don’t you just write it plain Java in @PostConstruct rather than over-relying on DI? Not everything has to be injected… The only advantage is that it makes testing easier since you can inject fake string and do not rely on current date. But in this case think of some DateProvider interface, makes life simpler.

    Also do you really want to have the same date for the whole application lifetime (it will be generated once at startup)? If not, currentDate bean must have prototype scope and you must lazily fetch it from the container every time you need it…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
i want to parse a xhtml file and display in UITableView. what is the
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.