Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Modifying a File's Last Modified Date

Java Example - Modifying a File's Last Modified Date

Java Examples Java Examples

The following example demonstrates how to use the File class's fileToChange.lastModified() and fileToChange.setLastModified() methods to modify a file's last modified date:

Main.java File

import

java

.

io

.

File

;

import

java

.

util

.

Date

;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

throws

Exception

{

File

fileToChange

=

new

File

(

"

C:/myjavafile.txt

"

)

;

fileToChange

.

createNewFile

(

)

;

Date

filetime

=

new

Date

(

fileToChange

.

lastModified

(

)

)

;

System

.

out

.

println

(

filetime

.

toString

(

)

)

;

System

.

out

.

println

(

fileToChange

.

setLastModified

(

System

.

currentTimeMillis

(

)

)

)

;

filetime

=

new

Date

(

fileToChange

.

lastModified

(

)

)

;

System

.

out

.

println

(

filetime

.

toString

(

)

)

;

}

}

The output of the above code is:



Sat Mar 21 22:00:48 CST 2015

true

Fri Apr 10 11:09:19 CST 2015

Java Examples Java Examples