• 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

Convert date format to timestamp

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
i have this date format 20060610000000 that is actually
2006/06/10 00:00:00 is there any way in java to convert it to unixtimestamp format?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java.text.SimpleDateFormat class can parse strings into Date objects, and also format Date objects into strings.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
allso to unix timestamp ?
is there any example somewhere in the net for converting to unix timestamp
not simple date ?
Thanks
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a description of how those timestamp strings look like that's no problem.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Meir-

As I understand it your problem has two parts:

1) How to convert a String representation of the date to a java.util.Date object.

2) How to print the java.util.Date object in the format on the Unix date command.

Here is the solution to part 2 of your problem.



Hope that helps,
Joshua Smith
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thanks for the example butthis is not the case (part 2)
say i will convert the string 20060610000000 to
2006
06
10
00
00
00

how can take this strings and buildfrom them unix timestamp
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using SimpleDateFormat you don't need to cut the string in pieces like you have shown. Just define a format that describes it, and then parse the input with it. That will give you a Date object which you can use for part 2 in the way Joshua showed.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i got it but yet something is wrong here
here is what i did to format it to unix timestamp



the result is : 1149886800

but when i check the result in this site :http://www.4webhelp.net/us/timestamp.php
im getting wrong date ..
why ?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That site seems to use GMT as a default, if you set your SimpleDateFormat to use GMT also, you will get the result you expect.


[ April 06, 2007: Message edited by: Garrett Rowe ]
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic