0

Today, If i run the program first time then create the object otherwise we use the previous object.

public class ObjectRestriction {

static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
static String date = "00000000";
public static void main(String[] args) {

    if( date.equals(formatter.format(new Date())))
    {
        System.out.println("already create object");
    }
    else
    {
        Reconcilation rc = new Reconcilation();
        date = formatter.format(new Date());
        System.out.println(date);
    }
} 

}

But when I run again then it initialize date variable to "00000000" and create new object. so, please help to restrict object creation on the basis of date.

Ashish
  • 110
  • 4
  • 14
  • 2
    your question is not clear to me – SpringLearner Jan 27 '17 at 08:04
  • Do you want to stop the program and restart later? – IQV Jan 27 '17 at 08:05
  • Instead of a `String`, use a `Date`, make `null` to begin with. If it's `null`, then create a new instance of the object. If `Date` is not `null`, you will need to create two more instances which fall either end of the day (0:00, 23:59) and check to see if the "now" falls between them, if it does, use the previously created object, otherwise, create a new object and assign "now" to the `Date` – MadProgrammer Jan 27 '17 at 08:08
  • You likely should serialise the date that has already been created and during the further launchings deserialise it and compare with a needed one. – Andrew Tobilko Jan 27 '17 at 08:08
  • 1
    Who keeps upvoting such low-level questions? Sorry, but **no** idea what you are asking. The code you are showing runs only **once**? Please step back, and read how to create a **reasonable** [mcve] that clearly points out what you intend to do and where it breaks. – GhostCat Jan 27 '17 at 08:18
  • @AndrewTobilko I really would **not** point a newbie who is lacking understanding of *everything* towards serialization here. We have no idea what he really wants to do. – GhostCat Jan 27 '17 at 08:19
  • Everytime you run your given code, it has no idea about previous run. your implementation for the logic is ok but not how you are testing it. If you can extract the logic in a method and call it from main with a for loop to run multiple times, you will see it working. – Monzurul Shimul Jan 27 '17 at 08:29
  • @GhostCat sir, I want only one object creation on the basis of date. But I didn't get because it initialize every time string date = starting value. – Ashish Jan 27 '17 at 08:30
  • 1
    No, what you want is many times more complicated than that. What you want is to **persist** the content of a variable from **one** invocation of the JVM to **another** completely different run of a JVM. In that case, Andrew is right with his assumption, but: this is **really really** advanced stuff. And it kinda looks like you are lacking even basic skills. – GhostCat Jan 27 '17 at 08:32
  • In that case, your question is answered here: http://stackoverflow.com/questions/447898/what-is-object-serialization – GhostCat Jan 27 '17 at 08:33
  • 1
    @AndrewTobilko : Thanks, my issue solved from your solution :) – Ashish Feb 03 '17 at 14:01

1 Answers1

2

A static variable does not survive the run of your app. Nor does any other kind of variable. After your main method completes its run, and you've not spawned any other threads, your app is done, complete, fini. All your variables are gone, wiped out, no longer in existence, cleared from memory.

Persistent storage

To save a value between runs of your app, you must persist the value, write the data to storage. For example in a file, or send to a database.

java.time

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes. For a date-only value without time-of-day and without time zone, use the LocalDate class.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

ISO 8601

The ISO 8601 standard defines sensible practical formats for text representing date-time values. Use these formats when writing your date-time objects to text files.

For date-only value that format is YYYY-MM-DD. The java.time classes use these standard formats by default. So you simply call toString.

String output = today.toString();

LocalDate ld = LocalDate.parse( "2017-01-23" );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154