如何在不使用日期函数的情况下将日期从 yyyy-MM-dd 转换为 dd-MMM-yyyy

发布于 2024-12-19 11:22:10 字数 586 浏览 3 评论 0原文

我有“2011-12-05”,我想将其转换为“2011 年 12 月 5 日星期一”。 我的日期转换代码取决于设备时区。如果我的时区是印度,那么我会得到日期Monday 05-Dec-2011,如果我的时区是牙买加金斯顿,我会得到Sunday 04-Dec-2011

因此,我的应用程序不会显示不同时区的正确日期。

是否有任何解决方案可以在没有 Blackberry Date 类或使用当前日期和时区的情况下转换日期?

我只想将此日期转换为 String

我正在使用以下函数转换此日期

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め七分饶幸 2024-12-26 11:22:10

您可以指定特定的区域设置,而不是依赖系统的默认设置。

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

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
吃颗糖壮壮胆 2024-12-26 11:22:10

DateTimePicker 根据位置或设备配置设置给出当前日期。

试试这个代码:
你应该得到你的要求。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文