Solved

how to convert date to timestamp

Posted on 2006-11-16
9
6,629 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
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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

687 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