简体   繁体   English

选择日期,月份和年份。file.lastModified();

[英]Pick day, month and year out file.lastModified();

I know how to use file.lastModified(); 我知道如何使用file.lastModified(); . When I println that I get (for example): Sat Mar 17 09:24:33 GMT+01:00 2012. But is it possible that I only get the day, month and year as numbers, for example: 17 03 2012 当我println ,我得到(例如):周六3月17日9点24分33秒GMT + 01:00 2012年。但它是可能的,我只得到了日,月,年的数字,例如:17 03 2012

Is their a way to do this, maybe a filter, or an other function to get last modified date in numbers? 他们是这样做的一种方法,可能是过滤器,还是其他函数来获取数字中的最后修改日期?

You can use SimpleDateFormat class, ie 您可以使用SimpleDateFormat类,即

package com.example.file;

import java.io.File;
import java.text.SimpleDateFormat;

public class GetFileLastModifiedExample
{
    public static void main(String[] args)
    {   
    File file = new File("\\somefile.txt");

    System.out.println("Before Format : " + file.lastModified());

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    System.out.println("After Format : " + sdf.format(file.lastModified()));
    }
}

is file a java.io.File object? 文件是java.io.File对象吗?

lastModified should return time in milliseconds. lastModified应该返回以毫秒为单位的时间。 You can create a Date object with the lastModified return value and the format the output with a SimpleDateFormat 您可以使用lastModified返回值创建Date对象,并使用SimpleDateFormat格式化输出

    Date date = new Date(file.lastModified());
    System.out.println(date);

    SimpleDateFormat sdf = new SimpleDateFormat("d M y");
    System.out.println(sdf.format(date));

Try this, 尝试这个,

long longDate = file.lastModified();        
Date date = new Date(longDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String newDate = formatter.format(date);
System.out.println("Formatted date " + newDate);

according to the documentation 根据文档

File.lastModifed() File.lastModifed()

should return a long value representing the time the file was last modified, measured in milliseconds since the epoch. 应该返回一个长值,该值表示文件的上一次修改时间,以自该时间段以来的毫秒数为单位。 Yon can use the long value in conjunction with Calendar Yon可以将long值与Calendar结合使用

    Calendar rightNow = Calendar.getInstance()
    rightNow.setTimeInMillis(longValue)

and use rightNow.get(...) 并使用rightNow.get(...)

to retrive day, month and year 检索日,月和年

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM