2014-04-28 127 views
0

我有表与数据的日期。如何通过本地化获取java中的数据?

这是我calcaulted日期

DateFormat dateFormat = getFormat(); 
date = dateFormat.parse((String) value).getTime(); 
if(date != null) { 
    cell.setValue(dateFormat.format(date)); 
    tableViewer.update(element, null); 
} 

public static DateFormat getFormat() { 
    String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$ 
    Locale locale = new Locale(systemLocale); 
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); 
    return dateFormat; 
} 

的日期是存在于屏幕格式月日(该月的名称),一年

for example Apr 26,2014. 

现在我想要得到的单元格的值并得到格式'yyyy-mm-dd' 该日期的值是2014年4月26日。 我如何得到2014-04-26的结果?我也认为,在UI的价值可能会改变根据用户的本地化

我试图

DateFormat dateFormat = getFormat(); 
    Date parse = dateFormat.parse((String) key); 

但随后get方法已过时,也没得到了getYear正确的结果

我不是专家的日期也许我错过了什么

+1

,可以储存在电池中的java.util.Date对象和[设置自定义渲染器绘制(http://docs.oracle.com/javase/tutorial/uiswing/components /table.html#renderer)以需要的(用户)格式。这样你就不必将字符串解析回日期。 – SebastianH

+0

我需要它在不同的格式,然后在用户界面。 – user1365697

+0

我需要它在一般,而不是表 – user1365697

回答

1

试试这个:

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

public class CustomFormattedDate { 

    public static void main(String[] args) { 
     Date date = new Date(); 
     // if you use DD for Daypattern, you'll get the day of the year, e.g. 119 for the 
     // 29th April 2014, if you want 29, the day of the month, use dd 
     SimpleDateFormat df = new SimpleDateFormat("YYYY-dd-MMM", new Locale(System.getProperty("user.language"))); 
     System.out.println(df.format(date)); 

     System.out.println(getDateFormat().format(date)); 
    } 
} 

输出

2014-29-Apr 
+0

我希望输出为YYYY-DD-MM,Locale.GERMAN是什么? – user1365697

+0

对不起,我更新了我的答案。 – Patrick

+0

我们可以聊天吗? – user1365697

相关问题