-1

I'm making this Login bit to a larger program, and I want to save the time of when a failed login happens and save the timestamp to a file called FailedLogins.txt. The problem is,"No suitable method found for write(Timestamp)", when I put the time stamp variable in the fwriter.write().

try {
        writer = new BufferedWriter( new FileWriter("UserPass.txt"));
        fwriter = new BufferedWriter( new FileWriter("FailedLogins.txt"));

        if(ppassword.equals(rrepassword)) {

            writer.write("Name: " + ffname + " " + llname + "\n");
            writer.write("Email: " + eemail + "\n");
            writer.write("Password: " + ppassword + "\n\n");
        }
        else {
            password.setText("");
            repassword.setText("");
            JOptionPane.showMessageDialog(null, "Your passwords do not match.");

            //For Failed Logins text File
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            fwriter.write(timestamp);
        }

    }
    catch ( IOException e) {
    }
    finally {
        try {
            if ( writer != null) {
                writer.close( );
            }
        }
        catch ( IOException e) {
        }
    }

Any Ideas?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
H. Woolverton
  • 31
  • 1
  • 7

3 Answers3

1

I guess you want this one:

fwriter.write("" + timestamp.getTime());
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
1

tl;dr

Instant.now().toString()

2018-01-06T22:33:12.123456Z

java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

To get the current moment in UTC, call Instant.now.

Instant instant = Instant.now() ;

ISO 8601

Generate a string in standard ISO 8601 format: YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ

String output = instant.toString() ;

2018-01-06T22:33:12.123456Z

Now you can pass that string to be written to your text file. Always use ISO 8601 formats when serializing date-time values as text.

As you can see, the java.time classes use the ISO 8601 standard formats by default. No need to specify a formatting pattern.

Going the other direction.

Instant instant = Instant.parse( "2018-01-06T22:33:12.123456Z" ) ;

File name

The colons are not allowed as a filename in some file systems such as HFS+, so replace with another character. I suggest some character other than the hyphens used in the date portion, so that you may reconstruct the ISO 8601 format if ever needed.

String output = instant.toString().replace( ":" , "_" ) ;  // Replace colon with another character for compatibility across file systems.

2018-01-06T22_33_12.123456Z


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
0

java.io.Writer.write() method overloads only accepts following arguments,

public void write(int c) throws IOException {...}

public void write(char cbuf[]) throws IOException {...}

abstract public void write(char cbuf[], int off, int len) throws IOException;

public void write(String str) throws IOException {...}

public void write(String str, int off, int len) throws IOException {...}

Therefore you don't have facility to provide Timestamp object as its parameter.

Its is possible to provide following string representation of Timestamp object.

fwriter.write(timestamp.toString());
// Or
fwriter.write(timestamp + "");
Saveendra Ekanayake
  • 3,153
  • 6
  • 34
  • 44