0

I have table with data of date.

This is how I calcaulted the date

 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;
}

The date is exist in the screen in format Month(name of the month) date, year

 for example Apr 26,2014.

Now I want to get the value of the cell and to get to format of 'yyyy-mm-dd' The value of the date is Apr 26,2014. How I can get the result of 2014-04-26 ? also I think that the value in the UI could change according to the localization of the user

I tried

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

but then all the get method are deprecated and also I didn't get right result for getYear

I am not expert in the date maybe I miss something

user1365697
  • 5,819
  • 15
  • 60
  • 96
  • 1
    You should store the java.util.Date object in the cell and [set a custom renderer to draw it](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer) in the required (user) format. That way you dont have to parse back the String into a Date. – SebastianH Apr 28 '14 at 12:26
  • I need it in different format then in the UI. – user1365697 Apr 28 '14 at 13:33
  • I need it in general and not for table – user1365697 Apr 28 '14 at 13:43
  • possible duplicate of [Format date in java](http://stackoverflow.com/questions/4772425/format-date-in-java) – SebastianH Apr 28 '14 at 16:34
  • The problem that the value is in format name of month day year hiw ican use simpleDAte in this case – user1365697 Apr 28 '14 at 17:50

1 Answers1

1

Try this:

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));
    }
}

Output

2014-29-Apr
Patrick
  • 4,532
  • 2
  • 26
  • 32