簡體   English   中英

如何在Java中解析具有MM / dd格式的日期

[英]How to parse date having MM/dd format in java

我是Java的初學者,我正在編寫一個程序以讀取多個文本文件,這些文件的數據如下所示:

[C417] ComputerName:KCUTSHALL-PC用戶ID:GO kcutshall Station 9900(已鎖定)LanId:| (11/23 10:54:09-11/23 10:54:44)| ping www.google.com [74.125.224.147]時,超出了平均限制(300)

我需要輸入用戶的日期范圍,並檢查輸入的日期是否在文件中的日期范圍之間,並顯示與日期相對應的計算機名稱。

我應該怎么做?

我嘗試使用SimpledDateFormat解析日期,但它給出了一個例外:

無法解析的日期(11/23 10:54:09-11/23 10:54:44)

這是我寫的代碼:

導入java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Scanner;
import java.util.*;
import java.text.*;

public class test {
  public static void main(String args[]) throws ParseException {
    try {
   Scanner input1=new Scanner(System.in);

   Scanner input2=new Scanner(System.in);
   System.out.println("Enter start date");
    String userDate1=input1.nextLine();
    System.out.println("Enter end date");
     String userDate2=input2.nextLine();
      //DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
DateFormat df = new SimpleDateFormat ("MM/dd");
      Date d1=df.parse(userDate1);
      Date d2=df.parse(userDate2);

      ZipFile zf=new ZipFile("C:\\Users\\Engineeir\\Desktop\\QoS_logs.zip");
      Enumeration entries=zf.entries();

      BufferedReader input=new BufferedReader(new InputStreamReader(
          System.in));
      while (entries.hasMoreElements()) {
        ZipEntry ze=(ZipEntry) entries.nextElement();

     BufferedReader br=new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
        String line;
        while ((line=br.readLine())!=null) {
              String[] st=line.split("\\|",-1);

              if(st.length>1)
             {
            String name=st[0];
            String dates=st[1];
DateFormat df1 = new SimpleDateFormat (" '('MM/dd '-' ')' ");
//String d3 = (Date)df1.parse(dates);
//SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd");
//Date d4 = newFormat.format(d3);
  //Date theDate = dateFormat.parse(d4); 

            Date d3=df1.parse(dates);

       if((d1.compareTo(d3)*d3.compareTo(d2))>0){
           System.out.println(name); }
else{
    System.out.println("Out of Range..Not found");
     }       
 }
           // br.close();



 }  } }catch (IOException e) {
      e.printStackTrace();
    }

}}
    String s = "11/23 10:54:09 - 11/23 10:54:44"; 
    String[] parts = s.split("-");
    DateFormat f = new SimpleDateFormat("MM/dd hh:mm:ss");
    Date d1 = f.parse(parts[0].trim());
    Date d2 = f.parse(parts[1].trim());

在這里,這可能會讓您介意

    public static String dates = "11/23 10:54:09 - 11/23 10:54:44";
        public static Pattern pattern = Pattern.compile("\\d\\d\\/\\d\\d \\d\\d:\\d\\d:\\d\\d");

        public boolean isDateOk() throws Exception {
            Matcher m = pattern.matcher(dates);
            SimpleDateFormat format = new SimpleDateFormat("MM/dd HH:mm:ss");
            Date startDate = null;
            Date endDate = null;
            Date ourDate = new Date();
            if(m.find()){
                startDate = format.parse(m.group());
            } else{
            throw new Exception("msg");
            }
            if(m.find()){
                endDate = format.parse(m.group());
            }else{
            throw new Exception("msg");
            }
            if (startDate!=null && endDate != null) {
                if (ourDate.after(startDate) && ourDate.before(endDate)){
                    return true;
                }
                return false;
            }


            return false;   
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM