问题描述
我有这个问题.问题:我正在制作一个程序,其中我通过 JFormatedTextField
接收用户输入,即字符串格式,然后我希望这个值是用户选择的文件的创建时间.
I have this problem.PROBLEM:I am making a program in which i am taking input from user via a JFormatedTextField
i.e. in string format and then i want this value to be creation time of a file chosen by user.
所以我需要使用 setTimes() 函数,它只接受 fileTime 格式.所以问题是:::: 如何将字符串转换为符合条件的文件时间,以便它可以被 .nio.attribute 中预定义的函数 setTimes() 使用.
So I need to uses setTimes() function which only accepts fileTime format.So the PROBLEM is:::: how do I convert the string into an eligible fileTime so that it can be used by the function setTimes() which is predefined in .nio.attribute.
http://www.docjar.com/docs/api/java/nio/file/attribute/FileTime.html
推荐答案
取决于输入的格式(我想它类似于 "dd/MM/yy HH:mm:ss"
),您可以使用 SimpleDateFormat
将其转换为 Date
,从 Date
您可以使用 Date.getTime> 获取毫秒()
并最终使用该值构建带有 java.nio.file.attribute.FileTime.fromMillis(long)
Depending on what the format of the input is (I suppose it is something like "dd/MM/yy HH:mm:ss"
), you can convert this to a Date
using SimpleDateFormat
, from the Date
you can get the milliseconds using Date.getTime()
and finally use that value to build a FileTime
with java.nio.file.attribute.FileTime.fromMillis(long)
应该这样做:
String text = textField.getText();
Date date = new SimpleDateFormat("dd/MM/yy HH:mm:ss").parse(text);
FileTime time = FileTime.fromMillis(date.getTime());
这篇关于在java中将字符串转换为FileTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!