面试遇到这样一个问题
1. 怎么把系统当前时间写入文件中?
按这个格式 yyyy-MM-dd HH:mm:ss2. 怎么读这个文件把它解析成一个DATE类型

解决方案 »

  1.   

    晕,居然有公司出这么简单面试题?这些都是用的很烂的代码了,就是日期和String互换
    Date d=new Date();
    SimpleDateFormat sf=new SimpleDateFormat("yy-mm-dd hh:mm:ss");
    String s=sf.parse(d);
      

  2.   

    import java.io.*;
    import java.util.*;
    import java.text.*;
    public class DateInFile{
    public static void main(String[] args){
    File file = new File("D:/train/csdn/date.txt");
    try{
    BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\train\\csdn\\date.txt"));
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = new Date();
    String msg = sdf.format(d);
    bw.write(msg);
    bw.flush();
    System.out.println(msg+"已写入文件!");
    bw.close();
    BufferedReader buf = new BufferedReader(new FileReader("D:\\train\\csdn\\date.txt"));
    String s = buf.readLine();
    Date dd = sdf.parse(s);
    System.out.println(dd+"已从文件取出!");
    buf.close();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    }
    }
      

  3.   

    2楼 答案不错 就是IO的操作 以及 date的 format
      

  4.   

    1楼的好像不对吧,parse方法怎么返回是String类型呢
      

  5.   

    C#代码,
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace ConvertUnicodeToByte
    {
        class IniFile
        {   
            static void Main(string[] args)
            {
                string riqi = "";
                string path = "";
                riqi = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
                //path = Application.StartupPath + "timedate.ini";
                path = @"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\ConvertUnicodeToByte\ConvertUnicodeToByte\bin\Debug\timedate.ini";
                WriteIniData("Now", "datetime", riqi, path);
                Console.WriteLine();
                Console.WriteLine(ReadIniData("Now", "datetime", "", path));
                Console.ReadLine();
            }        [DllImport("kernel32")]//返回0表示失败,非0为成功
            private static extern long WritePrivateProfileString(string section, string key,
                string val, string filePath);        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
            private static extern long GetPrivateProfileString(string section, string key,
                string def, StringBuilder retVal, int size, string filePath);        // 写入*.ini文件
            public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
            {
                if (File.Exists(iniFilePath))
                {
                    long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                    if (OpStation == 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }        // 读取*.ini文件中的相应节下面的键值
            public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
            {
                if (File.Exists(iniFilePath))
                {
                    StringBuilder temp = new StringBuilder(1024);
                    GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                    return temp.ToString();
                }
                else
                {
                    return String.Empty;
                }
            }    }
    }