Question

Im in a java class online and struggling with an assignment I have no idea how to do my teach asked us specifically:
"Create a GregorianCalendar object called dteDate. For year, month, date, hour, minute and seconds, extract those numbers from the file into separate integer variables using parseInt(). Then create your GregorianCalendar object. Finally, using SimpleDateFormat create a string called strDate that contains the "April 28, 2013 12:41". In other words, I want to see that you understand how to extract a series of strings from the file, convert them into integers, create a GregorianCalendar object then convert it back to a formatted string. Certainly not the most efficient way to do things but it will test your ability to create date variables."
I have no idea how to go about extracting specific line from a text file just how to extract the entire thing and would appreciate guidance my program looks like this.

import java.io.*;
import java.nio.file.*;
import java.util.GregorianCalendar;
public class a19010
{

public static void main(String[] args) 
  {
    Path objPath = Paths.get("dirsize.txt");  
    if (Files.exists(objPath))

    {
        File objFile = objPath.toFile();
        try(BufferedReader in = new BufferedReader(
                                new FileReader(objFile)))
        {
            String line = in.readLine();

            while(line != null)
            {
                System.out.println(line);
                line = in.readLine();
            }

        }    
      catch (IOException e)
      {
        System.out.println(e);  
      }
    }
    else
    {
     System.out.println(
             objPath.toAbsolutePath() + " doesn't exists");   
    }   

    GregorianCalendar dteDate = new GregorianCalendar();
  }
}

This prints out the entire text file to the console which was the first step of the assinment then I created the GregorianCalendar dteDate. Im unsure of how to extract the lines I need to provide the relevant information for my calender and what to do at this point in this program. I don't want to have to much words in this post and make my question harder to answer so ill post the relevant lines from the text file and if I need to add more information into my question or take some out or clarify let me know and I will edit it. The lines with the date and time in the text file are the first 4 lines in it and are as follows:(put in like code to make it clear)

The current date is: Thu 03/28/2013    
Enter the new date: (mm-dd-yy)  
The current time is: 12:41:26.31
Enter the new time:  

If anyone knows what to do or can even point me in the right direction on how to go about doing this I would be grateful for the help.

This is the code I reached with Bhushan Bhangale's help but Im having trouble importing into my calender.

String line = in.readLine();
        int lineNum = 1;
        //extract the month
        String monthSubstring = line.substring(25,27);
        int month = Integer.parseInt(monthSubstring) -1 ;
        //extact the year
        String yearSubstring = line.substring(31,35);
        int year = (Integer.parseInt(yearSubstring));
        //extract the date
        String daySubstring = line.substring(28,30);
        int day = Integer.parseInt(daySubstring);

        while(line != null)
        {
            System.out.println(line);
            line = in.readLine();
            lineNum ++;

         if (lineNum == 3)
         {
           //extract the hour
            String hourSubstring = line.substring(21,23);
            int hour = Integer.parseInt(hourSubstring); 
            //extract the minute
            String minuteSubstring = line.substring(24,26);
            int minute = Integer.parseInt(minuteSubstring);
            //extract the second
            String secondSubstring = line.substring(27,29);
            int second= Integer.parseInt(secondSubstring);
           }
       } 
     GregorianCalendar dteDate = new GregorianCalendar(year, month, day, hour, minute, second);
        System.out.println(dteDate.getTime());  

The problem is that the Calender cant see the hour minute or second and im not sure how to make it import them

Was it helpful?

Solution

Since its class work i will give hints for you to proceed further.

To start with let's assume that the file format is fixed.

The first line has date and third line has time. In the while loop since you are reading line by line you can surely handle which one is first or third line.

Then from the first line using regex (preferable) or StringTokenizer take out the date, month and year. Ignore the day as its irrelevant.

Then from the third line againg using regex or tokenizer take out hour, minute and seconds.

Check the GregorianCalendar api to set this information and create the object.

Use SimpleDateFormat to define the required date format and pass the date to get the formatted date.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top