• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Converting MySql Date to Java Date and vice varca

Hi,

How can i format the MySql Date (yyyy-MM-dd) to  java(MM-dd-yyyy)? and vice varca in Java.


Thanks,
Ajay Varma
0
ajayvegesna02
Asked:
ajayvegesna02
  • 3
2 Solutions
 
for_yanCommented:
Use SimpleDateFormat
0
 
for_yanCommented:

String s0Date = "2011-05-06";

SimpleDateFormat f0 = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date dd = f0.parse(s0Date, new ParsePosition(0));

SimpleDateFormat f1 = new SimpleDateFormat("MM-dd-yyyy");

String s1Date = f1.format(dd);



0
 
for_yanCommented:
If those are always in exactly this format you can use just substrings or
StringTokenizer luker that:

StringTokenizer t = new StringTokenizer(s0Date,"-");

String s0 = t.nextToken();
String s1 = t.nextToken();
String s2 = t.nexToken();

String s1Date = s1 + "-" +s2 + "-" + s0;

But with SimpleDateFormat - it is more general,
easier to modify if they change input.

0
 
ajayvegesna02Author Commented:
Both are working fine.....

Thanks a lot....
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now