File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Best Way to parse a string to a unexpectable Date Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best Way to parse a string to a unexpectable Date Format " Watch "Best Way to parse a string to a unexpectable Date Format " New topic
Author

Best Way to parse a string to a unexpectable Date Format

YuenLian Wu
Ranch Hand

Joined: Nov 16, 2005
Posts: 73
Hi all,

I have a string variable that contains the date with the format like "20060216", and I want to use Java program to parse it to "2006-02-16". How can I do that in a quick manner ? I tried to do the following, but it gives me year of 1970. I will appreciate if you can explain the reason to me.



Thanks in advance

Transistor
[ February 17, 2006: Message edited by: YuenLian Wu ]
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
import java.util.*;
import java.text.*;

public class FormatDate {

public static void main(String args[])throws Exception{

Date dt = null;
SimpleDateFormat dtFormat1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat dtFormat2 = new SimpleDateFormat("yyyy-MM-dd");
dt = dtFormat1.parse("20060216");
System.out.println("Unformated date is " + dt);
System.out.println("Formated date is " + dtFormat2.format(dt));
}
}


Sharing is learning
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best Way to parse a string to a unexpectable Date Format