Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.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 将格式从输入默认值更改为第D个月,YYYY_Java_Date_For Loop_Format_Date Formatting - Fatal编程技术网

Java 将格式从输入默认值更改为第D个月,YYYY

Java 将格式从输入默认值更改为第D个月,YYYY,java,date,for-loop,format,date-formatting,Java,Date,For Loop,Format,Date Formatting,0 我正在为我的学校作业制作一个约会课程,基本上做一些基本的功能,就像API Date一样。除了一个设置为当前本地时间的构造函数外,其他方法都不允许与Java的API日期类关联。然后,我提出了一种基于输入选项更改格式的方法。代码运行得很好,但是format方法的月输出计算有误。也许我对数组的逻辑做得不对,但无法找出正确的方法。 例如:如果我输入2019年4月2日并选择选项B,那么它将打印“2019年12月2日”而不是“4月” 我尝试了month=0和1的循环,但没有成功 import java.

0

我正在为我的学校作业制作一个约会课程,基本上做一些基本的功能,就像API Date一样。除了一个设置为当前本地时间的构造函数外,其他方法都不允许与Java的API日期类关联。然后,我提出了一种基于输入选项更改格式的方法。代码运行得很好,但是format方法的月输出计算有误。也许我对数组的逻辑做得不对,但无法找出正确的方法。 例如:如果我输入2019年4月2日并选择选项B,那么它将打印“2019年12月2日”而不是“4月”

我尝试了month=0和1的循环,但没有成功

import java.util.*;
public class Date
{
    // declare needed variables
   private int day;
   private int month;
   private int year;

   /**
    * Default constructor to set the date info to the current date 
    */
   public Date()
   {
       // I have trouble assign Date using the Java API Date class
       Date d1 = new Date();
       Calendar cal = Calendar.getInstance();
       cal.setTime(d1);
       day= cal.get(Calendar.DAY_OF_MONTH);
       month = cal.get(Calendar.MONTH);
       year = cal.get(Calendar.YEAR);
    }
    /**
    * Overloaded constructor to set the date info based on user input
    * @param int inMonth to input month value
    * @param int inDay to input day value
    * @param int inYear to input year value
    *
    */
    public Date( int inMonth, int inDay, int inYear)
    {
       //set all the inputs into suitable variables
       day = inDay;
       month = inMonth;
       year = inYear;
    }
    public String getDate(char format)
    {
        //String Array for option B

        String[] mArray = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
        String dayString = new String();
        //Switch code with param input format option and return the String based on it
        switch(format)
        {
            case 'A':
                dayString = month+"/"+day+"/"+year;
            break;
            case 'B':
            for(month = 1;month<mArray.length;month++)
            {

                dayString = mArray[month]+" "+day+" "+year;
            }
            break;
        }
        return dayString;
    }
    /**
    * ToString method to return date info
    * @return String of Date
    */
     public String toString()
    {
        //Return value of Date in String
        return month+"/"+day+"/"+year;
    }
}
import java.util.*;
公课日期
{
//声明所需的变量
私人国际日;
私人整数月;
私人国际年;
/**
*将日期信息设置为当前日期的默认构造函数
*/
公开日期()
{
//我在使用JavaAPI日期类分配日期时遇到问题
日期d1=新日期();
Calendar cal=Calendar.getInstance();
校准设定时间(d1);
day=cal.get(日历,每月的第天);
月=cal.get(日历月);
年=校准获取(日历年);
}
/**
*重载构造函数以根据用户输入设置日期信息
*@param int inMonth输入月份值
*@param int inDay输入日值
*@param int inYear输入年份值
*
*/
公开日期(月内、日内、年内)
{
//将所有输入设置为合适的变量
一天=一天;
月=月;
年=年;
}
公共字符串getDate(字符格式)
{
//选项B的字符串数组
字符串[]mArray={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
String dayString=新字符串();
//使用param input format选项切换代码并返回基于该选项的字符串
开关(格式)
{
案例“A”:
dayString=月+“/”+日+“/”+年;
打破
案例“B”:

对于(月=1;月这是选项B的代码:

        for(month = 1;month<mArray.length;month++)
        {

            dayString = mArray[month]+" "+day+" "+year;
        }
假设您的月份从1月份的1到12月份的12,您没有得到准确的数组元素,但您很快就会发现,因此我将由您自己来解决。无论如何,您需要一个明确的决定:在您的类中,1月份表示为0还是1?

我觉得这很好。我将添加几个想法:类名和java.time

类命名 将名为
Date
的类与现有类
Date
混合使用会带来麻烦。请使用其他名称

java.time 替换此项:

   // I have trouble assign Date using the Java API Date class
   Date d1 = new Date();
   Calendar cal = Calendar.getInstance();
   cal.setTime(d1);
   day= cal.get(Calendar.DAY_OF_MONTH);
   month = cal.get(Calendar.MONTH);
   year = cal.get(Calendar.YEAR);
…使用现代java.time类

该类表示一个只包含日期的值,不包含一天中的时间,也不包含或

时区对于确定日期至关重要。对于任何给定的时刻,全球各地的日期都因区域而异。例如,年午夜后几分钟是新的一天,而年仍然是“昨天”

如果未指定时区,JVM将隐式应用其当前默认时区。该默认时区可能在运行时(!),因此结果可能会有所不同。最好将所需/预期时区明确指定为参数。如果关键,请与用户确认该时区

请以
大陆/地区
的格式指定a,如
美国/蒙特利尔
非洲/卡萨布兰卡
,或
太平洋/奥克兰
。切勿使用2-4个字母的缩写,如
EST
IST
,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)

如果您想使用JVM的当前默认时区,请请求它并将其作为参数传递。如果省略,代码将变得模棱两可,我们无法确定您是否打算使用默认时区,或者您是否像许多程序员一样不知道这个问题

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.
回到你的代码

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;  // If crucial, confirm with the user.
LocalDate today = LocalDate.now( z ) ;
this.day = today.getDayOfMonth() ;
this.month = today.getMonthValue() ; // Sanely numbered 1-12 for January-December. Tip: better to use the `Month` enum objects rather than mere integer.
this.year = today.getYear() ;

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

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

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

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

从哪里获得java.time类

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

    • 对于早期的Android(仅供参考),诸如和
      java.text.simpleDataFormat
      等非常麻烦的日期时间类现在被java 8和更高版本中内置的类所取代。请参阅。
      ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.
      
      ZoneId z = ZoneId.of( "Africa/Tunis" ) ;  // If crucial, confirm with the user.
      LocalDate today = LocalDate.now( z ) ;
      this.day = today.getDayOfMonth() ;
      this.month = today.getMonthValue() ; // Sanely numbered 1-12 for January-December. Tip: better to use the `Month` enum objects rather than mere integer.
      this.year = today.getYear() ;