java - 如何在java中解析MM/dd格式的日期

标签 java date

我是 Java 初学者,我正在编写一个程序来读取多个文本文件,这些文件的数据如下:

[C417] ComputerName:KCUTSHALL-PC UserID:GO kcutshall Station 9900 (locked) LanId: | (11/23 10:54:09 - 11/23 10:54:44) | Average limit (300) exceeded while pinging www.google.com [74.125.224.147] 8x

我需要输入用户的日期范围,并检查输入的日期是否在文件中的日期范围之间,并显示与该日期对应的计算机名称。

我该怎么办?

我尝试使用 SimpledDateFormat 解析日期,但它给出了异常:

Unparsable date (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());

关于java - 如何在java中解析MM/dd格式的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904070/

相关文章:

python - 如何在 matplotlib 中使用日期时间更改 x 轴的范围?

php - MySQL 获取考虑不同月份长度的月度发票记录

java - 使用 Google Sheets Api 时出错

java - 如何在 Web 上运行 Apache Spark 作业后获取输出

java - mysql连接超时后无法从apache DBCP连接池获取连接

java - 字符串中的日期到日期中的日期转换

java - 通知一系列事件完成的监听器(启动同步服务)

java - 如何使用 Jackson ObjectMapper 和 Spring 的 RestTemplate 处理各种响应?

Android - 如何将通知设置为将来的特定日期?

javascript - 奇怪的 Javascript 日期差异