Tuesday, April 9, 2013

Selenium :Extracting Date from input string or putting date in particular format

                           Accepting String calender data and extracting date



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

//to create a SimpleDateFormat object and use it to parse Strings to Date and
//to format Dates to Strings. If you've tried SimpleDateFormat and it didn't work, then please show your code and any errors you may receive.
//
//Addendum: "mm" in the format String is not the same as "MM". Use MM for months and mm for minutes. Also, yyyyy is not the same as yyyy.

public class Changing_Date_Format
{

 public static void main(String[] args) throws ParseException
 {
  String date_s = "2011-01-18 00:00:00.0";

        // *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss" 
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dt.parse(date_s); //Converting String to Date

        // *** same for the format String below
        SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(dt1.format(date));

 }
}

No comments:

Post a Comment