Solved

how to convert date to timestamp

Posted on 2006-11-16
9
6,621 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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