Take the 2-minute tour ×

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)
                line = in.readLine();

      catch (IOException e)
             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)
            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);

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

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
Thanks for the help my confusion comes at the step in the while loop since it cycles through all the lines how do I get it to stop at a specific line in order to access it such as the first or third? –  Alex Chapp Apr 19 '13 at 5:52
Create the GregorianCalendar object before the while loop. In the while loop when you read first line call method say parseDate(calendar, line) and pass the calendar and line to that method. Similarly when you find third line call method parseTime(calendar, line). In those methods parse the string as required and set the Calendar attributes. After the while loop your calendar will be set with the read information. –  Bhushan Bhangale Apr 19 '13 at 6:13
I figured out how to do the first line but what im having trouble with is stopping the while loop as a specific line in order to implement a method I can only do the first one since its outside the while loop I came up with a new method but I get the feeling Its not propper coding form and it wouldn't reliably work with different data Ill edit it into my top post so you can see it –  Alex Chapp Apr 19 '13 at 6:20
Dude start a counter and after every line read increment the counter. Add a if or switch clause which check when counter is 1 call parseDate() method and when its 3 calls parseTime() method. Read about counter, if and switch to learn how to do it. Once you achieve all via whatever you understand the code can be improved later. –  Bhushan Bhangale Apr 19 '13 at 6:32
Alright so define your hour, minute, seconds variable before while loop and you should be fine. If you define a variable inside {} then its only available inside it, its known as scope. –  Bhushan Bhangale Apr 19 '13 at 9:07
show 6 more comments

Your Answer


