将日期从yyyy-mm-dd转换为日月日期

Hit*_*rth 5 blackberry date date-format java-me

我有这种格式的约会2011-11-02.即日起,我们怎么能知道Day-of-week,Month而且Day-of-month,像这种格式Wednesday-Nov-02,从日历或任何其他方式?

Boh*_*ian 13

如果它是普通的java,你将使用两个SimpleDateFormats - 一个读取,一个写入:

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);
Run Code Online (Sandbox Code Playgroud)

输出:

Wednesday-Nov-02
Run Code Online (Sandbox Code Playgroud)

作为一个函数(即静态方法),它看起来像:

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));
}
Run Code Online (Sandbox Code Playgroud)

警告:
不要试图做readwrite成静态字段保存实例化他们的每方法调用,因为SimpleDateFormat的是不是线程安全的!

编辑

但是,在咨询了Blackberry Java 5.0 API文档后,似乎该write.format部分应该可以与Blackberry的SimpleDateFormat一起使用,但是您需要使用其他东西来解析日期... HttpDateParser看起来很有前途.我没有安装JDK,但试试这个:

public static String reformat(String source) {
    SimpleDateFormat write = new SimpleDateFormat("EEEE-MMM-dd");
    Date date = new Date(HttpDateParser.parse(source));
    return write.format(date);
}
Run Code Online (Sandbox Code Playgroud)