Solved

How to generate Timestamp from String (in Spring MVC)

Posted on 2008-10-01
6
4,778 Views
Last Modified: 2013-11-24
We have a class with two methods, one for returning current Timestamp and another for converting a String to Timestamp.
Our problem is that we can't seem to get the convertion right.

This is what our ouput is:
Dates.getTimeStamp()  -->  returns correctly the current Timestamp
Dates.convertToTimestamp(2008, 12, 31, 24, 25, 0, 0)  --> returns 2009-01-01 00:25:00.0

Does anybody know what's wrong with the convertion?

CODE:
import java.sql.Timestamp;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
 
public class Dates {
  private Date date;
  private Timestamp timestamp;
  private Calendar calendar = Calendar.getInstance();  
 
  public Dates(){}
 
  //Return current Timestamp
  public Timestamp getTimeStamp() {
    timestamp = new Timestamp(System.currentTimeMillis());
    return timestamp;
  }
 
  //Converts ints to Timestamp
  public Timestamp convertToTimestamp(int year, int month, int date, int hour, int minute, int second, int nano){
		
    String tempDate = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + "00.00";
    System.out.println("String timestamp: " + tempDate);
 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    java.util.Date myDate = null;		
    Timestamp myTimestamp = null;
		
    try {
      myDate = sdf.parse(tempDate);
      myTimestamp = new Timestamp(myDate.getTime());
    } catch (ParseException e) {
      e.printStackTrace();
    }
		
    return myTimestamp;
  }
}

Open in new window

0
Comment
Question by:temj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22612248
> System.out.println("String timestamp: " + tempDate);

I don't this output is in line with the format you are using. Why don't you use java.util.Calendar. This class
is meant for doing manipulation with date. You can look at section "Representing a point in time in a Calendar" of
this article,

http://www.odi.ch/prog/design/datetime.php
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 50 total points
ID: 22612403
> System.out.println("String timestamp: " + tempDate);

I don't this output is in line with the format you are using. Why don't you use java.util.Calendar. This class
is meant for doing manipulation with date. You can look at section "Representing a point in time in a Calendar" of
this article,

http://www.odi.ch/prog/design/datetime.php
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22612411
sorry for double post.
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
LVL 20

Accepted Solution

by:
Venabili earned 200 total points
ID: 22612884
Hold on a second.
I may be having a slow day but what exactly is wrong here?
>>Dates.convertToTimestamp(2008, 12, 31, 24, 25, 0, 0)  --> returns 2009-01-01 00:25:00.0

The valid values for hours are 0 to 23 (not 1 to 24). So when you give 24, it just removes 24 from the hours and add 1 to the day (because you ask for 24:15 on a day that has time from 0:00 to 23:59) -- it is a standard conversion.... So when you say 24:25 at 31/12/2008 it is 00:25 at 1/1/2009

If this is your complaint here - the method works as it is supposed to....
0
 
LVL 20

Expert Comment

by:Venabili
ID: 22613024
By the way, if you want a different way for representing the hours in your pattern, these are the options:
H  - Hour in day (0-23)
k - Hour in day (1-24)
K - Hour in am/pm (0-11)
h - Hour in am/pm (1-12)

Using h and passing 24 is prone to strange results (or strange looking).. If you really want to have 1-24, use k...
0
 

Author Comment

by:temj
ID: 22640497
Ajay-Singh: Thanks for the link. Good reading og helpful hints for really understanding this subject.

Venabili: Right on the spot. Changing from "k" to "H" solved the problem. If anybody had a slow day, it was me :)
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question