Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert date format to timestamp  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • 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?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.text.SimpleDateFormat class can parse strings into Date objects, and also format Date objects into strings.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • 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: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a description of how those timestamp strings look like that's no problem.
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • 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
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • 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: 42972
73
  • Mark post as helpful
  • send pies
  • 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.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • 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 ?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!