Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
How to convert Data to Unix_Timestamp
Tarun Kumar Agrawal
Greenhorn
Posts: 17
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I want to convert date to unix_timestamp. Suppose if i give 11-June-07 as input, i want to get 1181648109265 as output. How should i do it?
What i am doing right now is:
import java.util.*; import java.text.*; import java.sql.Timestamp; public class DateToTimestamp { public static void main(String[] args) { try { String str_date="11-June-07"; DateFormat formatter ; Date date ; formatter = new SimpleDateFormat("dd-MMM-yyyy"); date = (Date)formatter.parse(str_date); java.sql.Timestamp timeStampDate = new Timestamp(date.getTime()); System.out.println("Today is " +timeStampDate); } catch (ParseException e) { System.out.println("Exception :"+e); } } }
What changes should i make to get required output?
Rob Spoor
Sheriff
Posts: 22783
131
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You already use that value, with date.getTime().
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Tarun Kumar Agrawal
Greenhorn
Posts: 17
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
date.getTime() gives -61932556800000 value, when i am storing in Long variable. It's different from expected value.
Rob Spoor
Sheriff
Posts: 22783
131
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Your input is incorrect. The 07 means the year 0007. Either change the input to "11-June-2007", or change the
pattern
to "dd-MM-yy".
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Tarun Kumar Agrawal
Greenhorn
Posts: 17
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I changed the input (although that too is correct). still same error
Rob Spoor
Sheriff
Posts: 22783
131
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Please post your full new code. Because when I tried changing either the input or the pattern I got the desired result.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Tarun Kumar Agrawal
Greenhorn
Posts: 17
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
oh! My Bad. Sorry.. It's working now. Thanks a lot
Rob Spoor
Sheriff
Posts: 22783
131
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You're welcome.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
convert a string field to date not working as expected.
Converting string to date with extra strings
SQL Column Date Type insertion problem
string to date conversion
how to change a date in desired format
More...