Solved

how to convert date to timestamp

Posted on 2006-11-16
9
6,605 Views
Last Modified: 2012-06-27
i have  a date which should be added by one day then  add xtd time to that date and finally  should be in Timestamp varaible.
 
 int addoneday =1;
 String xtd ="12:12"
 SimpleDateFormat  formatter = new SimpleDateFormat("dd-MMM-yy");
     Date   date = (Date)formatter.parse("29-Jan-02");
    Calendar c = Calendar.getInstance();
   
    c.setTime(date);
   
    c.add(c.DATE,addoneday);
      Timestamp ts   =      new Timestamp(new SimpleDateFormat("ddMMMyy/ HH:mm").parse(c.geTime()).getTime());
0
Comment
Question by:chaitu chaitu
  • 5
  • 3
9 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
>  Timestamp ts   =     new Timestamp(new SimpleDateFormat("ddMMMyy/
> HH:mm").parse(c.geTime()).getTime());


should be

Timestamp ts = new Timestamp(c.getTimeInMillis());
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
SimpleDateFormat  formatter = new SimpleDateFormat("dd-MMM-yy");
     Date   date = (Date)formatter.parse("29-Jan-02");
    Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, 1);
String[] timeFields = xtd.split(":");
c.add(Calendar.HOUR, Integer.parseInt(timeFields[0]));
c.add(Calendar.MINUTES, Integer.parseInt(timeFields[1]));
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
getting below exception

Exception in thread main
java.text.ParseException: Unparseable date: "Wed Jan 30 00:00:00 GMT+05:30 2002 12:12"



int xtd =1;
 SimpleDateFormat  formatter = new SimpleDateFormat("dd-MMM-yy");
     Date   date = (Date)formatter.parse("29-Jan-02");
    Calendar c = Calendar.getInstance();
   
    c.setTime(date);
   
    c.add(c.DATE,xtd);

    String estime = c.getTime() + " 12:12";
   
    Timestamp x=      new Timestamp(new SimpleDateFormat("ddMMMyy HH:mm").parse(estime).getTime());
    System.out.println(x);
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Use the code i posted then

Timestamp ts = new Timestamp(cal.getTimeInMillis());
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
CEHJ

when i use this i am getting this format 2002-01-30 12:12:00.0;

how to this format 30-Jan-02 12:12???


Timestamp ts = new Timestamp(c1.getTimeInMillis());
    System.out.println(ts);
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>how to this format 30-Jan-02 12:12???

Use the format


dd-MMM-yy HH:mm
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
  Timestamp ts = new Timestamp(new SimpleDateFormat("dd-MMM-yy hh:mm").parse(formatter2.format(c1.getTime())).getTime());
   

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You don't need to do any formatting/parsing if the source of the Date is the Calendar
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now