-2

In my JDeveloper 12c application I have an year saved as a number. I want to get this value and set it as an academic year. For example I type 2005 and setYear will change it into 2005/06.

At this moment, in my application I have field year saved as number and field yearacad as string. yearacad uses:

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

but it doesn't work like I wanted to - in my web application it doesn't update its value. At second run it updates, but it should immediately. I was consulting and I got an information that if I use getYear and setYear, it will work.

Can somebody help me with these Java methods?

@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);
}

Now I have error with my other class

       public void put(DeesconfViewRowImpl obj, Object value) {
            obj.setRokakStr((String) value);
        }
pnuts
  • 58,317
  • 11
  • 87
  • 139

2 Answers2

0

Try this:

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);
}
Alexander_Winter
  • 314
  • 1
  • 3
  • 9
  • Also one more question - I have a SQL value on YearAcad in my VO Rokak || '/' || substr((Rokak + 1),3,2) should I delete it or leave it like it is? – Wojciech Kalinski Apr 17 '14 at 12:24
  • If you need it in your database leave it in, but I think it's obsolete, so you should remove it. – Alexander_Winter Apr 17 '14 at 12:30
  • Removed, without class setYearAcad() my academic years looks like from 2000 to 204701. – Wojciech Kalinski Apr 17 '14 at 12:35
  • I don't know where you call your functions so I can't know if you need them or not. It's your code and nobody should know it better then you. If something doesn't work like it should, then debug your code step-by-step to find the error. – Alexander_Winter Apr 17 '14 at 12:37
  • I'll get it to myself Alex. Thanks for your help. Unfortunately in ADF it's not that easy to debug like it. Now I don't know why, but instead of saving it as from 2000 to 2000/01 it makes it like 204701 - it addes a 47 to my year and adds as string last two digits from next year. – Wojciech Kalinski Apr 17 '14 at 13:24
  • You know what 47 is? It's the ASCII-decimal for '/'. So the output is not wrong. Changed my code, try it again. – Alexander_Winter Apr 18 '14 at 08:40
  • Yea, I checked it today and I fixed it, solution was very easy - instead of '/' I had to put "/". Thanks a lot for help. WK – Wojciech Kalinski Apr 18 '14 at 08:53
  • Yep I know, was my fix as well. ;) – Alexander_Winter Apr 18 '14 at 08:59
0

java.time

The java.time classes built into Java 8, Java 9, and later provide these helpful classes:

  • Year class to represent a year
  • DateTimeFormatter class to create strings representing a date-time value in a specific format.
  • FormatStyle enum to flag longer or shorter formats.

The Year class might seem simplistic and gratuitous at first, but using it makes your code more self-documenting than passing around mere integer numbers.

You can use those to help in representing your academic year. Indeed, you might want to create your own class such as seen below. Look to the toString format methods specifically to answer the Question.

Using the class.

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

Implementation of 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 );
}

Full class.

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 ) );
    }
}

Academic year now: 2017/18


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154