BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to get the last modified date of a file in java

By Chaitanya Singh | Filed Under: java

Here we will learn how to get the last modified date of a file in java. In order to do this we can use the lastModified() method of File class. Following is the signature of this method.

public long lastModified()

It returns the time that the file denoted by this abstract pathname was last modified. The value returned by this method is in milliseconds so in order to make it readable, we can format the output using SimpleDateFormat.

Complete code:

Here we are fetching the last modified date of file “Myfile.txt” which is present in drive “C”. Since the value returned by method is not readable, we are using format() method of SimpleDateFormat class to format it.

import java.io.File;
import java.text.SimpleDateFormat;
public class LastModifiedDateExample
{
    public static void main(String[] args)
    {	
        //Specify the file path and name
	File file = new File("C:\\Myfile.txt");
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	System.out.println("Last Modified Date: " + sdf.format(file.lastModified()));
    }
}

Output:

Last Modified Date: 01/03/2014 22:41:49

We can format and display the output in any desired format. For example if we use the following pattern:

SimpleDateFormat sdf2 = new SimpleDateFormat("MM-dd-yy HH:mm a");
System.out.println("Last Modified Date: " + sdf2.format(file.lastModified()));

We will get the below output for above pattern:

Last Modified Date: 01-03-14 22:41 PM

There are several other patterns which you can use to get the output in desired form. To read more about date formatting refer SimpleDateFormat javadoc.

Comments

  1. rahul says

    November 24, 2015 at 7:28 AM

    hi
    could you please help me in writing a java code for copy text written using Mangal.ttf font in one text file to another.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap