Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将日历java中的月份字符生成为变量_Java_Calendar_Simpledateformat_Monthcalendar_Gettime - Fatal编程技术网

将日历java中的月份字符生成为变量

将日历java中的月份字符生成为变量,java,calendar,simpledateformat,monthcalendar,gettime,Java,Calendar,Simpledateformat,Monthcalendar,Gettime,大家好 我试图使用Calendar java创建一个日期变量,下面是我的示例代码: long day = System.currentTimeMillis(); Calendar cal = Calendar.getInstance(); java.text.SimpleDateFormat month = new java.text.SimpleDateFormat("MMM"); // not sure how to assign this value inside cal.setTimeI

大家好

我试图使用Calendar java创建一个日期变量,下面是我的示例代码:

long day = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
java.text.SimpleDateFormat month = new java.text.SimpleDateFormat("MMM"); // not sure how to assign this value inside
cal.setTimeInMillis(day);
cal.set(Calendar.HOUR_OF_DAY, 00);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 00);
cal.set(Calendar.MILLISECOND, 000);
Date todayDate = cal.getTime();

Timestamp current = new Timestamp(todayDate.getTime());
但是,我得到的
current
的值是
2014-01-13 00:00:00.0
。我宁愿将
01
设置为
Jan


有什么想法吗?

你的意思是这样的

        long day = System.currentTimeMillis();
        Calendar cal = Calendar.getInstance();
        java.text.SimpleDateFormat month = new java.text.SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:S"); // not sure how to assign this value inside
        cal.setTimeInMillis(day);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 000);
        Date todayDate = cal.getTime();

        Timestamp current = new Timestamp(todayDate.getTime());
        System.out.println(month.format(current));
收益率:

2014年1月13日00:00:00:0


请查看更多的格式选项。

您是指类似的内容吗

        long day = System.currentTimeMillis();
        Calendar cal = Calendar.getInstance();
        java.text.SimpleDateFormat month = new java.text.SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:S"); // not sure how to assign this value inside
        cal.setTimeInMillis(day);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 000);
        Date todayDate = cal.getTime();

        Timestamp current = new Timestamp(todayDate.getTime());
        System.out.println(month.format(current));
收益率:

2014年1月13日00:00:00:0

请检查以获取更多格式选项。

您可以尝试此选项

    long day = System.currentTimeMillis();
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat month = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:S");
    cal.setTimeInMillis(day);     
    cal.set(Calendar.HOUR_OF_DAY, 00);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 00);
    cal.set(Calendar.MILLISECOND, 000);
    Date todayDate = cal.getTime();

    Timestamp current = new Timestamp(todayDate.getTime());
    System.out.println(month.format(current));
现在付诸表决:

    2014-Jan-13 00:00:00:0
你可以试试这个

    long day = System.currentTimeMillis();
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat month = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:S");
    cal.setTimeInMillis(day);     
    cal.set(Calendar.HOUR_OF_DAY, 00);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 00);
    cal.set(Calendar.MILLISECOND, 000);
    Date todayDate = cal.getTime();

    Timestamp current = new Timestamp(todayDate.getTime());
    System.out.println(month.format(current));
现在付诸表决:

    2014-Jan-13 00:00:00:0

//不确定如何在内部分配此值

好了……)

SimpleDataFormat类的附加

//不确定如何在内部分配此值

好了……)


除了SimpleDataFormat类之外,由于您正在查找当前日期,因此不需要
currentTimeMillis()

相反,您可以执行以下操作以获得所需的输出

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss.S");
System.out.println(sdf.format(cal.getTime()));

由于您正在查找当前日期,因此不需要
currentTimeMillis()

相反,您可以执行以下操作以获得所需的输出

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss.S");
System.out.println(sdf.format(cal.getTime()));
tl;博士 避免麻烦的遗留日期时间类。仅使用java.time类

一月

java.time 现代方法使用java.time类

获取当前月份意味着确定当前日期。获取当前日期需要一个时区,因为对于任何给定的时刻,日期在全球各地都会因区域而异

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate ld = LocalDate.now( z ) ;
Month m = ld.getMonth();
让枚举自动本地化月份的名称。通过传递一个

四月

日期与时间 如果您的目标是将一天中的某个时间与日期结合起来,请使用
ZoneDateTime
class。必须指定时区以确定一天中的第一个时刻。在某些区域的某些日期,一天不是从00:00:00开始的

ZonedDateTime zdt = ld.atStartOfDay( z ) ;  // Determine the first moment of the day.
不要使用
java.sql.Timestamp
来生成字符串。使用
DateTimeFormatter
类,该类记录在关于堆栈溢出的许多其他答案中

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MMM-dd HH:mm:ss" , Locale.US ) ;
String output = zdt.format( f ) ;
或者只需调用
ZonedDateTime::toString
以生成标准ISO 8601格式的字符串,并明智地扩展以在方括号中附加区域名称

String output = zdt.toString() ;
实际上,根本不要使用时间戳。该类用于数据库,从JDBC 4.2及更高版本开始,现在由
Instant
取代


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

LocalDate.now()                  // Determine the current date as seen in the wall-clock time in use by JVM’s current default time zone. Better to specify explicitly your desired/expected time zone.
         .getMonth()             // Get the `Month` enum object representing the month of that `LocalDate` object’s date value.
         .getDisplayName(        // Let the `Month` enum object automatically localize to generate the string of the name of the month.
             TextStyle.SHORT ,   // Specify how long or abbreviate you want the text of the name of the month.
             Locale.US           // Specify a `Locale` to determine the human language and cultural norms to be used in localizing the name of the month.
         )                       // Return a String.
从哪里获得java.time类

  • 然后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类
    • 对于早期的Android(tl;dr 避免麻烦的遗留日期时间类。仅使用java.time类

      一月

      java.time 现代方法使用java.time类

      获取当前月份意味着确定当前日期。获取当前日期需要一个时区,因为对于任何给定的时刻,日期在全球各地都会因区域而异

      ZoneId z = ZoneId.of( "America/Montreal" );
      LocalDate ld = LocalDate.now( z ) ;
      Month m = ld.getMonth();
      
      让枚举自动本地化月份名称。通过传递

      四月

      日期与时间 如果您的目标是将一天中的某个时间与日期相结合,请使用
      ZoneDateTime
      class。您必须指定一个时区来确定一天中的第一个时刻。在某些区域的某些日期,一天不会从00:00:00开始

      ZonedDateTime zdt = ld.atStartOfDay( z ) ;  // Determine the first moment of the day.
      
      不要使用
      java.sql.Timestamp
      来生成字符串。请使用
      DateTimeFormatter
      类,该类在堆栈溢出的许多其他答案中都有说明

      DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MMM-dd HH:mm:ss" , Locale.US ) ;
      String output = zdt.format( f ) ;
      
      或者只需调用
      ZonedDateTime::toString
      以生成标准ISO 8601格式的字符串,并明智地扩展以在方括号中附加区域名称

      String output = zdt.toString() ;
      
      实际上,根本不要使用
      时间戳
      。该类用于数据库,从JDBC 4.2及更高版本开始,现在被
      即时
      取代


      关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,如,&

      该项目现已启动,建议迁移到类

      要了解更多信息,请参阅.和搜索堆栈溢出以获取许多示例和解释。规范为

      您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
      java.sql.*
      classes

      LocalDate.now()                  // Determine the current date as seen in the wall-clock time in use by JVM’s current default time zone. Better to specify explicitly your desired/expected time zone.
               .getMonth()             // Get the `Month` enum object representing the month of that `LocalDate` object’s date value.
               .getDisplayName(        // Let the `Month` enum object automatically localize to generate the string of the name of the month.
                   TextStyle.SHORT ,   // Specify how long or abbreviate you want the text of the name of the month.
                   Locale.US           // Specify a `Locale` to determine the human language and cultural norms to be used in localizing the name of the month.
               )                       // Return a String.
      
      从哪里获得java.time类

      • 然后
        • 内置的
        • 标准JavaAPI的一部分,带有捆绑实现
        • Java9添加了一些次要功能和修复
        • 大部分java.time功能都在中向后移植到Java6和Java7
        • 更高版本的Android捆绑包实现了java.time类

        • 对于早期的Android(你想要像01 currentMonth这样的日期吗?我更喜欢它变成
          2014-Jan-13 00:00:00.0
          ,而不是
          2014-01-13 00:00.0
          。仅供参考,像、
          java.text.SimpleDateFormat
          这样麻烦的旧日期时间类现在可以使用了