Solved

how to convert date to timestamp

Posted on 2006-11-16
9
6,613 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
 
LVL 86

Expert Comment

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

Timestamp ts = new Timestamp(cal.getTimeInMillis());
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 77
Starting to learn JAVA, 7 59
javap bin 2 24
couple of eclipse 5 16
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

930 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

11 Experts available now in Live!

Get 1:1 Help Now