0

I have "2011-12-05" and I want to convert this to "Monday 05-Dec-2011". My date conversion code depends on the device timezone. If my timezone is India, then I get date Monday 05-Dec-2011 and if my timezone is Kingston, Jamaica, I get Sunday 04-Dec-2011.

For this reason my application does not display the correct date for different timezones.

Is there any solution to convert date without Blackberry Date class or using current Date and Time Zone?

I want to only convert this date to String

I am converting this date using below function

public static String reformatMonthDate(String source) 
{
    SimpleDateFormat write = new SimpleDateFormat("dd MMM yyyy"); //YYYY-MMM-dd
    Date date = new Date(HttpDateParser.parse(source));
    return write.format(date);
}
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Hitarth
  • 1,950
  • 3
  • 27
  • 52
  • You're doing it the wrong way. The former variant is compliant with ISO-8601. There's no reason to jumble it up. ;-) – onemasse Dec 05 '11 at 11:40
  • i dont want to user any Locale and default .. it gives me just coverting date .. – Hitarth Dec 05 '11 at 13:28
  • my app is based on Event according to date .. and i m getting event in different different Date for different country ..event is same .. – Hitarth Dec 05 '11 at 13:29
  • If it is working fine then why you want another method.....??? Why you waste your time to find new method.?? – V.J. Dec 06 '11 at 04:58
  • please see my above comment .. i had issue when i converting date in for different location – Hitarth Dec 06 '11 at 06:00

2 Answers2

2

You can specify a specific locale, instead of relying on the system's default.

Locale locale = Locale.get(Locale.LOCALE_fr);

// Parse with HttpDateParser
Date date = new Date(HttpDateParser.parse("2002-01-29"));

// Format with a custom format and locale
DateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy", locale);
StringBuffer buf = new StringBuffer(30);
String s = formatter.format(date, buf, null).toString(); // mar., 29 janv. 2002
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
  • Surprisingly, BlackBerry doesn't have SimpleDateFormat.parse(). http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/i18n/SimpleDateFormat.html – Michael Donohue Dec 06 '11 at 06:46
0

DateTimePicker gives the current date according to the location or device configuration settings.

Try this code: You should get your requirement.

public class LoadingScreen extends MainScreen implements FieldChangeListener
{
private String select_Date[]={"Select Date"};
private String month_ar[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
private ObjectChoiceField choiceField;
private ButtonField show;
public LoadingScreen() 
{
    createGUI();
}
private void createGUI()
{
    choiceField=new ObjectChoiceField("Select Date: ", select_Date, 0)
    {
        protected boolean navigationClick(int status, int time) 
        {
            DateTimePicker datePicker = DateTimePicker.createInstance( Calendar.getInstance(), "dd/MM/yyyy",null);
            datePicker.doModal();
            Calendar cal1=datePicker.getDateTime();
            String day="";
            String mon="";
            if(String.valueOf(cal1.get(Calendar.DAY_OF_MONTH)).length()==1)
            {
                 day="0"+String.valueOf(cal1.get(Calendar.DAY_OF_MONTH));
            }
            else
            {
                 day=String.valueOf(cal1.get(Calendar.DAY_OF_MONTH));
            }
            mon=String.valueOf(cal1.get(Calendar.MONTH));
            String month=""+month_ar[cal1.get(Calendar.MONTH)];
            select_Date[0]=day+"-"+month+"-"+cal1.get(Calendar.YEAR);
            choiceField.setChoices(select_Date);
            return true;
        }
    };
    add(choiceField);
    show=new ButtonField("Show",FIELD_HCENTER);
    show.setChangeListener(this);
    add(show);
}
public void fieldChanged(Field field, int context) 
{
    if(field==show)
    {
        UiApplication.getUiApplication().invokeLater(new Runnable() 
        {
            public void run() 
            {
                Dialog.alert(select_Date[choiceField.getSelectedIndex()]);
            }
        });
    }
}
public boolean onMenu(int instance) 
{
    return true;
}
protected boolean onSavePrompt() 
{
    return true;
}
}

OutPut

alishaik786
  • 3,696
  • 2
  • 17
  • 25