Converting MySql Date to Java Date and vice varca

Posted on 2011-05-06
Last Modified: 2012-05-11

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

Ajay Varma
Question by:ajayvegesna02
    LVL 47

    Expert Comment

    Use SimpleDateFormat
    LVL 47

    Accepted Solution


    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);

    LVL 47

    Assisted Solution

    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.


    Author Closing Comment

    Both are working fine.....

    Thanks a lot....
    LVL 92

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now