Aditya
Aditya

Reputation: 547

How to forcefully cast a string(generated by formating some date) into Date?

I have a string (Jan12) (generated by applying some operations on current date {20-jan-2012}) Now i want to convert back this string into Date format . Also the value should be same i.e the new Date object should have value jan12 and not (20-jan-2012) . Pls help . I have tried doing

 java.sql.Date.valueOf("Jan12") [this throws IllegalArgumentException]

and also

new SimpleDateFormat("MMMyy").parse("Jan12") [By this Date gets converted to 20-jan-2012]

Output required : A Date Object having value Jan12 (12 is the year) My Code : new java.text.SimpleDateFormat("MMMyy").format(new java.text.SimpleDateFormat("yyyy-MM-dd").parse(s)) // It is a string which gives Jan12

Now i really want to convert Mycode into a Date object

    Date now = new Date();                       
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
    String s1 = df.format(now); 
    System.out.println(s1);    // 2012-01-20
    java.sql.Date d111=java.sql.Date.valueOf(s1);
    System.out.println(d111); // 2012-01-20
    DateFormat df1 = new SimpleDateFormat("MMMyy");
    String s2 = df1.format(d111);
    System.out.println(s2);  //Jan12
  Now i want s2 to be converted in Date object 

Upvotes: 1

Views: 555

Answers (7)

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

@Aditya,

If you use the Str2 which gives "Jan12", there is no date part in that string and therefore if you convert it to a date object, it will get "Jan" as month, 12 as year but it cant find "day" in that String.

if you use below code

 try
    {
            Date d2 = df1.parse(s2); //here s2 is your string which gives "JAN12"
            System.out.println(d2);
    }
    catch(ParseException pe)
    {
        System.out.println("parse exception..");
    }

The output to the above code will be:

Sun Jan 01 00:00:00 IST 2012

notice here that day part is reset to the first day of the month

Therefore, it is not possible to get a complete date object as your original Date, the month and year are preserved, but the day part is lost.

Upvotes: 1

Emanuel Landeholm
Emanuel Landeholm

Reputation: 1408

Just extend Date and customize it to use your favourite parse & format methods.

Upvotes: 0

bongi
bongi

Reputation: 41

I understand that you want to format your Date object into a String representation. You can use SimpleDateFormat for this, analog to your second example:

Date d = new Date(112, 0, 20); //don't construct a date like this in production code, use a Calendar instance instead
String formattedDate = new SimpleDateFormat("MMMyy").format(d); // -> "Jan12"

Note that your Date object represents a specific point in time, it will always have a day and a time associated with it. If you want to compare Dates with the resolution of a month, you have to set day and time to neutral values:

Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
d = cal.getTime();

Upvotes: 0

user425367
user425367

Reputation:

If 12 is a year

Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("MMMyy").parse("Jan12"));
calendar.set(Calendar.DATE, 1);
Date date = calendar.getTime(); // First Jan 2012

If 12 is a day

Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("MMMdd").parse("Jan12"));
calendar.set(Calendar.YEAR, 2012);
Date date = calendar.getTime(); // 12 Jan 2012

Upvotes: 0

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

Use the SimpleDateFormat class properly, it will do exactly what you want

String str_date="12-Jan-2012";
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); 
Date date = (Date)formatter.parse(str_date);  

Note: the formatter.parse() method throws ParseException, catch it;

Upvotes: 0

Bohemian
Bohemian

Reputation: 425328

So the 12 is day, not a year - you should parse it as such. Aslo, you'll need to tell it what year this is:

System.out.println(new SimpleDateFormat("yyyyMMMdd").parse("2012" + "Jan12"));

Output

Thu Jan 12 00:00:00 EST 2012

Upvotes: 0

Emanuel Landeholm
Emanuel Landeholm

Reputation: 1408

What do you mean "gets converted"? How your Date is displayed is a separate issue. Look into formatting a Date.

Upvotes: 0

Related Questions