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

  • Home
  • SEARCH
  • 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 6927449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:03:26+00:00 2026-05-27T11:03:26+00:00

We’ve a public static util method which can parse a string and return a

  • 0

We’ve a public static util method which can parse a string and return a Date object, but it also throws ParseException in case the string parsed cannot be converted to a Date object.

Now, in another class I would like to have a static final Date initialized to a value using the util method described above. But given that the util method throws ParseException, this is not allowed.

This is what I want to do, which is not allowed

public static final MY_DATE = Util.getDateFromString('20000101');

What is the recommended way to keep this Date field ‘final’?

  • 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-27T11:03:27+00:00Added an answer on May 27, 2026 at 11:03 am

    Well you could use a static initializer block:

    public static final Date MY_DATE;
    
    static {
        try {
           MY_DATE = Util.getDateFromString("20000101");
        } catch (ParseException e) {
           // Whatever you want to do here. You might want to throw
           // an unchecked exception, or you might want to use some fallback value.
           // If you want to use a fallback value, you'd need to use a local
           // variable for the result of parsing, to make sure you only have a
           // single assignment to the final variable.
        }
    }
    

    However, I would advise against this. Date is a mutable type – exposing it via a public static final variable is a bad idea.

    As of Java 8, the java.time package is the most appropriate to use for almost all date/time work, where you’d write:

    public static final LocalDate START_OF_JANUARY_2000 = LocalDate.of(2000, 1, 1);
    

    Prior to Java 8, I’d recommend that you use Joda Time which has many immutable date/time types – and is a thoroughly better library for working with dates and times. It looks like you’d want:

    public static final LocalDate START_OF_JANUARY_2000 = new LocalDate(2000, 1, 1);
    

    Note that even if you do decide to go with java.util.Date, it doesn’t make much sense to parse the string in my view – you know the values numerically, so why not just supply them that way? If you haven’t got a suitable method to construct a Date from a year / month / day (presumably applying an appropriate time zone) then you could easily write one.

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

Sidebar

Related Questions

public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.