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 5969891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:17:44+00:00 2026-05-22T20:17:44+00:00

So I get a date attribute from an incoming object in the form: Tue

  • 0

So I get a date attribute from an incoming object in the form:

Tue May 24 05:05:16 EDT 2011

I am writing a simple helper method to convert it to a calendar method, I was using the following code:

    public static Calendar DateToCalendar(Date date ) 
{ 
 Calendar cal = null;
 try {   
  DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
  date = (Date)formatter.parse(date.toString()); 
  cal=Calendar.getInstance();
  cal.setTime(date);
  }
  catch (ParseException e)
  {
      System.out.println("Exception :"+e);  
  }  
  return cal;
 }

To simulate the incoming object I am just assigning the values within the code currently using:

private Date m_lastActivityDate = new Date();

However this is givin me a null pointer once the method reaches:

date = (Date)formatter.parse(date.toString()); 
  • 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-22T20:17:45+00:00Added an answer on May 22, 2026 at 8:17 pm

    Here’s your method:

    public static Calendar toCalendar(Date date){ 
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      return cal;
    }
    

    Everything else you are doing is both wrong and unnecessary.

    BTW, Java Naming conventions suggest that method names start with a lower case letter, so it should be: dateToCalendar or toCalendar (as shown).


    OK, let’s milk your code, shall we?

    DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    date = (Date)formatter.parse(date.toString()); 
    

    DateFormat is used to convert Strings to Dates (parse()) or Dates to Strings (format()). You are using it to parse the String representation of a Date back to a Date. This can’t be right, can it?

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

Sidebar

Related Questions

When I use DateTime.Now I get the date and time from the server point
How do I get today's date in C# in mm/dd/yyyy format? I need to
I would like to get up-to-date information on Google's index of a website, and
How do I get the modified date/time of a file in Python?
Is there a way to get notification of date change in c#? I have
I've used the localtime function in Perl to get the current date and time
For example, I am trying to get a min date, a max date, and
How to get a file's creation date or file size, for example this Hello.jpg
What does it mean when you get or create a date in UTC format
Was looking to get peoples thoughts on keeping a Lucene index up to date

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.