Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert date from yyyy-mm-dd to day-month-date

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?

like image 452
Hitarth Avatar asked Nov 02 '11 05:11

Hitarth


People also ask

How do you convert MM DD to YYYY month?

Two extract the value for Month, first note that we have to cut the first four digits off so that yyyymmdd becomes mmdd. Then, dividing mmdd by 100 yields mm. This is done with MOD(Number,10000)/100, where MOD(Number,10000) retrieves mmdd and this result is divided by 100 yielding Month.

How do I change the date format from YYYY to MM DD in Excel?

First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.

How do I convert date to month and day in Excel?

Select the cells you want to format. Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.


1 Answers

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!

Edited

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);
}
like image 99
Bohemian Avatar answered Jan 01 '23 09:01

Bohemian