android - 将月份转换为 MMM 格式

标签 android calendar

我正在尝试使用 SimpleDateFormat 将我的月份转换为 MMM 格式,但无法转换。

    tvDisplayDate = (TextView) findViewById(R.id.dateDisplay);

    Calendar cal=Calendar.getInstance();

    SimpleDateFormat format = new SimpleDateFormat("MMM");

    int tempyear = cal.get(Calendar.YEAR);
    int tempmonth = cal.get(Calendar.MONTH);
    int tempday = cal.get(Calendar.DAY_OF_MONTH);

    String month = new Integer(tempmonth).toString();

最佳答案

以下作品(我刚刚测试过)

Calendar cal=Calendar.getInstance();
int tempmonth = cal.get(Calendar.MONTH);

SimpleDateFormat newformat = new SimpleDateFormat("MMM");
SimpleDateFormat oldformat = new SimpleDateFormat("MM");

        String monthName = null;
        Date myDate;
        try {
            myDate = oldformat.parse(String.valueOf(tempmonth));
            monthName = newformat.format(myDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Log.d("MMM", monthName);

关于android - 将月份转换为 MMM 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702334/

相关文章:

android - 在 android 中创建聊天应用程序的最佳实践是什么

c++ - 用于解析 C++ 中重复日期描述的库?

asp.net - 禁用 asp 上的 future 日期 :calendar control

Java 日历日期操作问题

android - 使用 Ionic 处理 ZipAlign(Android 构建)

android - 我们可以为 android.os.DownloadManager 设置自定义通知吗?

java - 公历 : Jumping from tuesday to sunday

android - 如何获取所有当前的android日历实例

Android NFC 卡模拟/使用手机作为卡

android - kotlin/java中的匹配方法名