Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best Way to parse a string to a unexpectable Date Format

 
YuenLian Wu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic