Results 1 to 6 of 6

Thread: Time method

  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default Time method

    I'm busy writing a program that creates a database of all you songs and movies.
    It displays your songs in a list with Track name, Artist, Album and date added. I could have it so that it asks the date each time, but this is a bit tedious, so I was wondering which method I could use to get the date and store as a String...

    A bit example code explaining it would be great.

    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    import java.text.*;
    
    public class GettingTheDate {
        public static void main(String[] args) {
            DateFormat df = new SimpleDateFormat("dd MMM yyyy");
            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();
            String toStore = df.format(now);
            System.out.printf("now = %s%ntoStore = %s%n", now, toStore);
        }
    }

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Ok. sweet. Thanks for your help. That should do it. And just one more little question. When I output the songs in columns (currently just using System.out.println), is there any way besides tabs to keep the words properly lined up? I've had to make checks that add extra tabs if the words are below a certain length... Thanks again

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    private String space(int n) {
        String s = "";
        for(int j = 0; j < n; j++)
            s += " ";
        return s;
    }

  5. #5
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Ok, thanks. But then I still have to specify how long I want the spaces to be. Here's my code for the output part:

    Java Code:
    System.out.println("\n\n\n#\tTrack\t\t\tArtist\t\t\tAlbum\t\t\tDate Added\n------------------------------------------------------------------------------------------");
    for (int i = 0; i <= songNum; i++) System.out.println(i + "\t" + song[i][0] + "\t\t\t" + song[i][1] + "\t\t\t" + song[i][2] + "\t\t\t" + song[i][3]);
    I had to add checks earlier in the program to add/remove extra tabs depending on the length of the word...

    Thanks for your help

  6. #6
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Oops. Sorry, my bad. Thinking like a idiot. Just needed to minus the word length from the max length to get the int parameter.
    Shot!

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  2. Hello, first time here.
    By ludragon in forum Introductions
    Replies: 2
    Last Post: 01-03-2008, 06:03 AM
  3. Method execution time
    By javaplus in forum Advanced Java
    Replies: 3
    Last Post: 11-26-2007, 10:51 AM
  4. DataObject with the time given by me
    By garinapavan in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 07:33 PM
  5. Time restricted method
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •