call staic method with instance class (LocalDate - java

my question is about the class LocalDate have a static method of that return a new instance of the classe example
LocalDate local = LocalDate.of(2019,9,21);
the probleme is that i can't call the method of with instance class localDate
(local.of(..) !!!!!!!!!???)
thanks for help :)

The static method LocalDate of(int year, int month, int dayOfMonth) Obtains an instance of LocalDate from a year, month and day. so you must use a reference of a LocalDate object to store the returned instance.
LocalDate local = LocalDate.of(2019,9,21);

Related

How to create the object of class LocalDate in Java? And why not use new keyword?

I am using this code to create the object of class LocalDate in Java.time package but it does not require the keyword new. Can anyone tell me how this exactly works.
LocalDate date = LocalDate.of(year, month, day);
The LocalDate class has a private constructor. So you cannot instantiate a new instance of the LocalDate class using the new keyword. But the LocalDate class implements a static function of, which lets you specify the year, month, and day of the month, and the function returns the new constructed LocalDate object.
LocalDate provides a static method named of that allocates a new instance.
Any class can do this.
class MyClass {
:
static MyClass makeOne(int someArg) {
:
MyClass thing = new MyClass();
:
return thing;
}
:
}
makeOne might equally be named of if that makes sense.
So, you're right, 'new' is how instances get created, but that doesn't mean the 'new' call is written in your code.

Java Custom Data Type

I wanted to create a class with a custom data type that returns the class object. Consider a class Custom:
public class Custom {
// Some fields.
public Custom(String custom) {
// Some Text.
}
// Some Methods.
public void customMethod() {
// Some Code.
}
}
Now, consider a second class TestCustom:
public class TestCustom {
public static void main(String[] args) {
Custom custom = new Custom("Custom");
System.out.println(custom); // This should print "Custom"
custom.customMethod(); // This should perform the action
}
}
So, the question how to get the value custom on instantiating an object instead of memory location. Like what I get is:
Custom#279f2327
The java.util.Date class returns the current date. This can be seen as the constructor for the class is
public Date() {
this(System.currentTimeMillis());
}
For example, the following code would print out the current date:
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
System.out.println(format.format(date));
The Answer by ML72 is correct and should be accepted. The java.util.Date constructor captures the current moment in UTC.
java.time
The java.util.Date class is terrible, for many reasons. That class is now legacy, supplanted years ago but the java.time classes as of the adoption of JSR 310.
The java.time classes avoid constructors, instead using factory methods.
The replacement for java.util.Date is java.time.Instant. To capture the current moment in UTC, call the class method .now().
Instant instant = Instant.now() ;
If you want the current moment as seen through the wall-clock time used by the people of a particular region (a time zone), use ZoneId to get a ZonedDateTime object. Notice again the factory method rather than a constructor.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Adjust to UTC by extracting an Instant.
Instant instant = zdt.toInstant() ;
Override the toString() method, as it is automatically invoked when you try to display an object:
Add a field. For example;
private String value;
In the constructor, add the following code:
value = custom;
this will assign a value passed to the constructor as a parameter, to the value field.
And finally override the toString() method as follows:
#Override
public String toString() {
return value;
}
Now, when you display the value of the custom object, the overridden toString() method will be invoked and the argument will be displayed instead of the memory address. Whereas methods of the object will work as they are programmed to work. There is nothing to be changed with them.

Why can LocalDateTime return the instance?

LocalDateTime is abstract class. So I cannot write:
LocalDateTime value = new LocalDateTime(); //error
If I want to get its instance, I have to write:
LocalDateTime value = LocalDateTime.now(); //not error
I have a question, Why can LocalDateTime return the instance? It's an abstract class.
I saw the overview, but I could not find it...
LocalDateTime is not an abstract class.
public final class LocalDateTime
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
It has private constructors, so direct instantiation is not possible. Factory method such now(), now(ZoneId) etc are used to create instances.
LocalDateTime is an immutable date-time object that represents a date-time.
This class does not store or represent a time-zone. Instead, it is a description of the date. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.
Hence it has static methods e.g.
LocalDateTime desc = LocalDateTime.now();

Static Variables Not Affecting Other Objects in Java Calendar Class

Since the Calendar class in Java has static data fields such as DATE, why don't the other objects change when the static field is modified?
I have made two different Gregorian Calendars and thought static data fields changed the value for all of the objects instead of one.
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TEST {
public static void main(String[] args) {
GregorianCalendar cal = new GregorianCalendar();
System.out.println(cal.get(Calendar.DATE));
GregorianCalendar cal2 = new GregorianCalendar();
cal2.set(Calendar.DATE, 12);
System.out.println(cal2.get(Calendar.DATE));
System.out.println(cal.get(Calendar.DATE));
}
}
Calendar.DATE is not a static field, it's a static variable that's used to reference which type of value you want to set/get in a specific Calendar instance.
If you look at the actual source code of java.util.Calendar you would see that it has an internal int array that holds all the values, i.e. day, month, year, etc.
Calendar.DATE is just a nice way of referencing the fifth element of that array.
Declaration of member in the Java 8 source code.
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* day of the month. This is a synonym for <code>DAY_OF_MONTH</code>.
* The first day of the month has value 1.
*
* #see #DAY_OF_MONTH
*/
public final static int DATE = 5;
The Answer by rorschach is correct and should be accepted.
Also, you are using old date-time classes that have proven to be poorly designed, confusing, and troublesome. Avoid them. They have been supplanted by the java.time classes.
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
LocalDate nextWeek = today.plusWeeks( 1 );
LocalDate midMonth = today.withDayOfMonth( 15 );
LocalDate firstOfMonth = today.with( TemporalAdjusters.firstDayOfMonth() );
LocalDate secondTuesdayOfThisMonth = today.with( TemporalAdjusters.dayOfWeekInMonth( 2 , DayOfWeek.TUESDAY ) );

LocalDate with Joda constructor says The constructor LocalDate(int, int, int) is not visible

According to the documentation in Joda:
public LocalDate(int year,
int monthOfYear,
int dayOfMonth,
Chronology chronology)
Should take the above which I did. I tried to set Chronology to null but I get the following error:
The constructor LocalDate(int, int, int, null) is undefined
However I am passing the correct values, but from what I understand the chronology null means ISOChronology in default time zone.
Therefore how can I pass three correct Integer values and use the constructor correctly?
if (cit.hasNext()) {
cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
if (DateUtil.isCellDateFormatted(cell))
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String cellValue = sdf.format(cell.getDateCellValue());
//System.out.println(cellValue);
//bufferDate is getting mm/dd/yyyy from excel cell
String[] dateSpliter = cellValue.split("/");
int month= Integer.parseInt(dateSpliter[0]);
int day= Integer.parseInt(dateSpliter[1]);
int year= Integer.parseInt(dateSpliter[2]);
_date = new LocalDate(year,month,day,null);
po.setDate(_date);
}
It looks to me as if you have imported the wrong class.
The Java 8 java.time.LocalDate class has no public constructors, but it does have a private constructor that takes three int values. I think this class is what you have imported by mistake, when you wanted org.joda.time.LocalDate instead.
I know that this question is old, but you can just write:
LocalDate date = LocalDate.of(int, int, int);
I hope somebody will find this helpfull.

Categories