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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:49:31+00:00 2026-06-07T02:49:31+00:00

I have the following code to convert different string representations of date/datetime into timestamp

  • 0

I have the following code to convert different string representations of date/datetime into timestamp object:

public class GeneralMethods {

    public static String[] formats = {"dd/mm/yyyy","mm/dd/yyyy hh:mm:ss","E MMM dd HH:mm:ss zzz yyyy"};

/**
 * a method to return timestamp object from string date in the formats 
 * {"dd/mm/yyyy HH:mm:ss","E MMM dd HH:mm:ss zzz yyyy","dd/mm/yyyy"} 
 * @param date: the date to format in string representation
 * @return timestamp object
 */
public static Timestamp timestampFormat(String date)
{
    Date dateObj = new Date(); 
    for(String format: formats)
    {
         try 
         {
             dateObj =  new SimpleDateFormat(format).parse(date);
                        System.out.println(dateObj.toString()); // for tracking

         }catch (ParseException e) 
         {

         }  
    }
    return new Timestamp(dateObj.getTime());

}

   // testing
   public static void main (String args[])
  {
    System.out.println(timestampFormat("05/31/2011 21:37:35"));
  }

}

the output from this code is:

Wed Jan 05 00:31:00 GMT 2011
Mon Jan 31 21:37:35 GMT 2011
2011-01-31 21:37:35.0

as you can see the month in the output is wrong for the input 05/31/2011 21:37:35. how this is happening?

  • 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-06-07T02:49:33+00:00Added an answer on June 7, 2026 at 2:49 am

    mm is for minutes MM is for months so with your patterns you set minutes and months are set by default to January.

    Try changing your patterns to "dd/MM/yyyy", "MM/dd/yyyy hh:mm:ss"

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

Sidebar

Related Questions

I have the following code: public IDictionary<string, int> GetCountByDate(DateTime fromDate, DateTime toDate) { var
I have used following code to convert string to json and parse it. String
I have the following code that is meant to convert milliseconds into hours, mins
I have never used Foxpro before. Can you convert the following Foxpro code into
I have the following code to convert the type of the matrix using cv::Mat.convertTo()
I have the following code sample: float val = 16777216.0F; var badResult = Convert.ToDecimal(val);
I have the following FORTRAN code which I need to convert to C or
I have the following JavaScript code which I like to convert to jQuery but
I have a 'strange' problem, the following code converts the location lat value into
I have following piece of code: public void ProcessRequest (HttpContext context) { context.Response.ContentType =

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.