Reputation: 1950
I have date in this format 2011-11-02
. From this date, how can we know the Day-of-week
, Month
and Day-of-month
, like in this format Wednesday-Nov-02
, from calendar or any other way?
Upvotes: 5
Views: 2005
Reputation: 424993
If it were normal java, you would use two SimpleDateFormats - one to read and one to write:
SimpleDateFormat read = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat write = new SimpleDateFormat("EEEE-MMM-dd");
String str = write.format(read.parse("2011-11-02"));
System.out.println(str);
Output:
Wednesday-Nov-02
As a function (ie static method) it would look like:
public static String reformat(String source) throws ParseException {
SimpleDateFormat read = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat write = new SimpleDateFormat("EEEE-MMM-dd");
return write.format(read.parse(source));
}
Warning:
Do not be tempted to make read
or write
into static fields to save instantiating them every method invocation, because SimpleDateFormat is not thread safe!
However, after consulting the Blackberry Java 5.0 API doc, it seems the write.format
part should work with blackberry's SimpleDateFormat, but you'll need to parse the date using something else... HttpDateParser looks promising. I don't have that JDK installed, but try this:
public static String reformat(String source) {
SimpleDateFormat write = new SimpleDateFormat("EEEE-MMM-dd");
Date date = new Date(HttpDateParser.parse(source));
return write.format(date);
}
Upvotes: 13