Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to convert String to Date object in java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-01-2010
Member
 
Join Date: Aug 2009
Posts: 56
How to convert String to Date object in java?
  

Hello to all,
I am new to this forum. I am just started learning java language. In last lecture our sir asked one question How to convert String to Date object in java? and none of us able to said the answer. That's why I am asking this question on this forum. Please help me.
Thank you.

Reply With Quote
  #2  
Old 20-01-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: How to convert String to Date object in java?

Hey it is easy process to convert String to Date object in java. Just look at following example to understand how to convert String to Date object in java.


Code:
import java.text.*;
import java.util.*;

public class DateFormat
{
  public DateFormat()
  {
    String dateS = "2009/04/10";
    
    SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/mm/dd");
    Date convertedD = dateFormat.parse(dateString);

    System.out.println("Converting string to date : " + convertedD);
  }

  public static void main(String[] argv)
  {
    new DateFormat();
  }
}



}
Reply With Quote
  #3  
Old 20-01-2010
Member
 
Join Date: Oct 2005
Posts: 2,388
Re: How to convert String to Date object in java?

You have to use parse(String text) method to do this process. This method is used to parse date/time string into date and the return type of this method is Date. I have written example of this type.


Code:
import java.util.*;
import java.text.*;
public class StringToDate {
   public static void main(String[] args) {
 try {    String strdate1="20-Jan-10";
         DateFormat F1 ; 
     Date d1 ; 
          F1 = new SimpleDateFormat("dd-MMM-yy");
              d1 = (Date)F1.parse(strdate1);    
               System.out.println("Date is " +d1 );
    } catch (ParseException e)
    {System.out.println("Exception :"+e);    }    
     
   }
}
Reply With Quote
  #4  
Old 20-01-2010
Member
 
Join Date: May 2008
Posts: 2,379
Re: How to convert String to Date object in java?

The following code shows how to convert String to Date object in java? In following example I had use java.text.SimpleDateFormat that extends java.text.DateFormat abstract class.
Code:
package org.kodejava.example.java.util;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
 
public class SToD
{
    public static void main(String[] args)
    {
        DateFormat DF = new SimpleDateFormat("dd/MM/yyyy");
  
        try
       {
            Date T1 = DF.parse("20/01/2010");           
            System.out.println("T1 = " + DF.format(T1));
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
    }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 20-01-2010
Member
 
Join Date: Feb 2008
Posts: 1,844
Re: How to convert String to Date object in java?

Following code example shows how to format a String to a Date object. In following example the method convertStringToDate actually takes three arguments as integers, date, month and year, and then concatenates these values to a String date.


Code:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/
public class Main {
    
    public void convertStringToDate(int d, int m, int y) {
        
        SimpleDateFormat dF = new SimpleDateFormat("dd/MM/yyyy");
 
        String in = d + "/" + m + "/" + y;
        try {
            
            Date theD1 = dF.parse(in);
            System.out.println("Date parsed = " + dF.format(theD1));
            
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
    }
    
    public static void main(String[] args) {
        new Main().convertStringToDate(20, 11, 2010);
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to convert String to Date object in java?"
Thread Thread Starter Forum Replies Last Post
How to convert string to char array in java? Baazigar Software Development 6 10-01-2011 05:36 PM
Convert string to binary in java TechGate Software Development 5 28-01-2010 12:36 PM
How to convert InputStream to String in java? KALIDA Software Development 4 20-01-2010 05:16 PM
How to convert string into int in Java Zool Software Development 3 09-11-2009 11:41 AM
how to convert a boolean value to a string in java? Janus Software Development 3 29-07-2009 08:07 PM


All times are GMT +5.5. The time now is 07:49 AM.