简体   繁体   English

在java中读取和写入日期/日历到txt文件

[英]Read and Write Date/Calendar to a txt file in java

How can I read and write a Date/Calendar to a txt file.如何将日期/日历读取和写入 txt 文件。 I want to store the map of Date,String key value pair to a text file and be able to restore it back into a map.我想将 Date,String 键值对的映射存储到文本文件中,并能够将其恢复回映射。 What I did right now is just looping through all of the map and write Date.tostring() + “," + string to the txt file but I don't know how to restore Date.tostring() to a Date, by the way, is there any possibility I only want year/month/date/hour/minutes but no time zone in my Date.tostring() and still restore it back into a Date object?我现在所做的只是遍历所有地图并将 Date.tostring() + “,” + string 写入 txt 文件,但我不知道如何将 Date.tostring() 恢复为 Date,通过方式,有没有可能我只想要年/月/日/小时/分钟,但我的 Date.tostring() 中没有时区,并且仍然将它恢复回 Date 对象?

You can save the time to txt file with the format you see in time variable below and then parse it using the rest of the code.您可以使用您在下面的time变量中看到的格式将时间保存到 txt 文件,然后使用其余代码对其进行解析。

String time = "Jul 24 2012 05:19:34";
DateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mm:ss");
Date date = df.parse(time);

You can use this method which creates a folder with a random name and then insert a date to a txt file there:您可以使用此方法创建一个具有随机名称的文件夹,然后将日期插入到 txt 文件中:

 try {
        Random rand = new Random();
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date today = Calendar.getInstance().getTime();
        String reportDate = df.format(today);
        String dateToPrintToFile = reportDate;
        File folder = new File("<your Folder>/" + rand);
        File file = new File("<your Folder>/" + rand + "/testDate.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(dateToPrintToFile);
        bw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

you can use SimpleDateFormat for formating date您可以使用 SimpleDateFormat 来格式化日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/HH/mm");
String line = sdf.format(date) + "," + text;

restore恢复

String[] l = line.split(",");
Date d = sdf.parse(l[0]);
String text = l[1];

If you don't want to deal with parsing complex human-readable date strings, the Date.getTime() function如果您不想处理解析复杂的人类可读的日期字符串,则Date.getTime()函数

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMTrepresented by this Date object.返回自 1970 年 1 月 1 日格林威治标准时间 00:00:00 以来由此 Date 对象表示的毫秒数。

In other words you can get this long value and write that to file (as a string) instead of a human-readable date string.换句话说,您可以获得这个long值并将其写入文件(作为字符串)而不是人类可读的日期字符串。 Then read the long (as a string) back in from the text file and instantiate with然后从文本文件中读回long (作为字符串)并使用

String[] l = line.split(","); //Split the line by commas
long value = Long.ParseLong(l[0]); //Parse the string before the comma to a long
Date readDate = new Date(value); //Instantiate a Date using the long

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

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