Java年份和学年

时间:2014-04-17 11:20:04

标签: java oracle oracle-adf jdeveloper

在我的JDeveloper 12c应用程序中,我将一年保存为数字。我希望得到这个价值并将其设定为学年。例如,我输入2005,setYear会将其更改为2005/06

此时,在我的应用程序中,我将字段年份保存为数字,将字段yearacad保存为字符串。 yearacad使用:

year || '/' || substr((year + 1),3,2)

但它不能像我想的那样 - 在我的Web应用程序中它不会更新它的值。在第二次运行它更新,但它应立即。我正在咨询,我得到的信息是,如果我使用getYearsetYear,它就可以使用。

有人可以帮助我使用这些Java方法吗?

@Alexander_Winter

public Integer getYear() {
    return (Integer) getAttributeInternal(YEAR);
}



public void setYear(Integer value) {
    setAttributeInternal(YEAR, value);
}


public String getYearAcad() {
    /*return YEAR || '/' || substr((YEAR + 1),3,2); */
 return (String) getAttributeInternal(YEARACAD); 
}



public void setYearAcad(String value) {
    setAttributeInternal(YEARACAD, value);
}

现在我的其他班级错误

       public void put(DeesconfViewRowImpl obj, Object value) {
            obj.setRokakStr((String) value);
        }

2 个答案:

答案 0 :(得分:0)

试试这个:

public Integer getYear() {
    return (Integer) getAttributeInternal(YEAR);
}

/**
 * Gets the attribute value for the calculated attribute YearAcad.
 * @return the YearAcad
 */
public String getYearAcad() {
 int year =  getYear();
 return String.valueOf(year) +  "/" + String.valueOf(year + 1).substring(2, 4);
}

// if you need to save the value
public void setYearAcad() {
 String value = getYearAcad();
 setAttributeInternal(YEARACAD, value);
}

答案 1 :(得分:0)

java.time

Java 8,Java 9及更高版本中内置的java.time类提供了这些有用的类:

Year类起初可能看起来过于简单和无偿,但使用它会使您的代码更自我记录而不是仅仅传递整数。

您可以使用这些来帮助代表您的学年。实际上,您可能想要创建自己的类,如下所示。请专门查看toString format方法来回答问题。

使用班级。

YearAcademic.now( ZoneId.of( "Africa/Tunis" ) )
            .format( FormatStyle.SHORT ) 

format的实施。

String s;
if ( ( formatStyle == FormatStyle.FULL ) || ( formatStyle == FormatStyle.LONG ) )      // Generate a string in format of YYYY/YYYY such as 2017/2018.
{
    s = this.start + "/" + this.stop;
} else if ( ( formatStyle == FormatStyle.MEDIUM ) || ( formatStyle == FormatStyle.SHORT ) )   // Generate a string in format of YYYY/YY such as 2017/18.
{
    s = this.start + "/" + this.stop.format( this.doubleDigitYearFormatter );
} else
{
    // Handle error, what should be unreachable code.
    s = "ERROR";
    System.out.println( "ERROR - Reached IF-ELSE for format style argument: " + formatStyle );
}

全班。

package com.example.javatimestuff;

import java.time.Year;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class YearAcademic
{
    Year start, stop;
    private DateTimeFormatter doubleDigitYearFormatter = DateTimeFormatter.ofPattern( "uu" );

    // Constructor
    public YearAcademic ( Year startArg )
    {
        this.start = startArg;
        this.stop = this.start.plusYears( 1L );
    }

    // Constructor
    public YearAcademic ( Year startArg , Year stopArg )
    {
        this.start = startArg;
        this.stop = stopArg;
    }

    // Constructor
    public YearAcademic ( int startArg , int stopArg )
    {
        this.start = Year.of( startArg );
        this.stop = Year.of( stopArg );
    }

    // Constructor
    public YearAcademic ( int startArg )
    {
        this.start = Year.of( startArg );
        this.stop = this.start.plusYears( 1L );
    }

    // Constructor
    public static YearAcademic now ( ZoneId zoneId )
    {
        YearAcademic ya = new YearAcademic( Year.now( zoneId ) );
        return ya;
    }

    @Override
    public String toString ( )
    {
        String s = this.format( FormatStyle.FULL  );
        return s;
    }

    public String format ( FormatStyle formatStyle )
    {
        String s;
        if ( ( formatStyle == FormatStyle.FULL ) || ( formatStyle == FormatStyle.LONG ) )      // Generate a string in format of YYYY/YYYY such as 2017/2018.
        {
            s = this.start + "/" + this.stop;
        } else if ( ( formatStyle == FormatStyle.MEDIUM ) || ( formatStyle == FormatStyle.SHORT ) )   // Generate a string in format of YYYY/YY such as 2017/18.
        {
            s = this.start + "/" + this.stop.format( this.doubleDigitYearFormatter );
        } else
        {
            // Handle error, what should be unreachable code.
            s = "ERROR";
            System.out.println( "ERROR - Reached IF-ELSE for format style argument: " + formatStyle );
        }
        return s;
    }

    public static void main ( String[] args )
    {
        YearAcademic ya = YearAcademic.now( ZoneId.systemDefault( ) );
        System.out.println( "Academic year now: " + ya.format( FormatStyle.SHORT ) );
    }
}
  

学年:2017/18

关于java.time

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

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

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore