Solved

how to convert date to timestamp

Posted on 2006-11-16
9
6,620 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
ID: 17957963
>  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
ID: 17957981
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
ID: 17957999
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17958023
Use the code i posted then

Timestamp ts = new Timestamp(cal.getTimeInMillis());
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17958063
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
ID: 17958145
>>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
ID: 17958531
  Timestamp ts = new Timestamp(new SimpleDateFormat("dd-MMM-yy hh:mm").parse(formatter2.format(c1.getTime())).getTime());
   

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17958606
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
ID: 17959806
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
expectj telnet failing 5 45
Why my table column Id is not passed to java object? 4 44
Unhandled exception type Exception 18 42
ejb on wildfly 5 30
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 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