【问题标题】:How to get the current month from Date如何从日期获取当前月份
【发布时间】:2014-08-01 06:41:11
【问题描述】:

我需要将 long 值转换为任何日期。接下来我想写出月份,转换为字符串。我想在 TextView 上显示月份。

我试过了:

Date dt = new Date();
Calendar cal= Calendar.getInstance();
cal.setTime(dt);  //get current time

long dateAsLong = calendar.getTimeInMillis(); // get currentTime as long

CalendarView.setDate(dateAsLong); // give the view CalendarView the current date 


TextView tv= (TextView) findViewById(R.id.TextViewDate);
tv.setText(String.valueOf(dateAsLong)); // <-- but this is wrong

【问题讨论】:

  • Java Convert Long to Date? 的可能重复项
  • 几乎,但主要问题是从转换后的日期获取月份名称。但是感谢您的奉献!

标签: java android


【解决方案1】:

tl;博士

将长值转换为…日期…写出的月份

Instant.ofEpochMilli( myMillis )
       .atZone( ZoneId.of( "Pacfic/Auckland" ) ) 
       .getMonth()
       .getDisplayName( TextStyle.FULL , Locale.ITALY )  // Or Locale.US, Locale.UK, etc.

奥伯里

避免使用旧的日期时间类

其他答案使用麻烦的旧日期时间类,它们现在是遗留的,被 java.time 类取代。

java.time

假设您的 long 值表示自 1970 年 UTC 第一刻以来的毫秒数,请使用 Instant

Instant 类表示UTC 中时间轴上的时刻,分辨率为nanoseconds(最多九 (9) 位小数)。

Instant instant = Instant.ofEpochMilli( myMillis ) ;

你想写一个月。定月,即定日。确定日期需要时区。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。

continent/region 的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

ZoneId z = ZoneId.of( "Africa/Tunis" ); 
ZonedDateTime zdt = instant.atZone( z ) ;  // Same moment in history, but adjusted into the wall-clock time of a particular region of people.

现在询问这个月。 java.time 类包括一个Month 枚举,表示一月至十二月。

Month m = zdt.getMonth() ;

Month 枚举包括方便的方法,例如以自动本地化格式生成字符串。

String output = m.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH )  ;  // Or Locale.US, Locale.ITALY, etc.

关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

从哪里获得 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

【讨论】:

    【解决方案2】:

    使用以下代码:

        long longDate=12334;
        Date date=new Date(longDate);
        String month=new SimpleDateFormat("MMMM").format(date); //you can use month for display
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:
          Date date = new Date();
          System.out.println(date.getTime());//Timestamp 
          long timestamp = date.getTime();
          date = new Date(timestamp);
          String month = new SimpleDateFormat("MMMM").format(date);//Get month string
          System.out.println(month);
      

      【讨论】:

        【解决方案4】:

        先从Long中获取日期

        long val = 1346524199000l;
        Date date=new Date(val);
        SimpleDateFormat df2 = new SimpleDateFormat("MMM");
        String month= df2.format(date);
        

        一旦你有字符串形式的月份,使用下面的代码将它转换成 android textView

        textview.setText(month);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多