4 Replies - 888 Views - Last Post: 21 January 2013 - 05:43 PM Rate Topic: ***-- 2 Votes

#1 bobjob5  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 21-January 13

Format Dates Using SimpleDateFormat

Posted 21 January 2013 - 03:18 PM

Hi everyone,

My programming class just started and we're learning Java. Here is the challenge our teacher gave us:

> build a date de-assembler from scratch
> takes today’s date and display it separately in dialog windows (JDialog): year, month (the name of the month, NOT the number), day, hour, minutes, seconds, milliseconds
> ADVANCED : do this using only ONE SimpleDateFormat Object


My problem is the advanced part. Since I'm such a newbie with it, I copy and pasted most of it so it would work the way he wanted. There might be a much simpler way, but since I'm so new to Java, we haven't really learned how to get rid of redundancy.

Anyone have any pointers on the advanced part? I'm not necessarily looking for the answers, although that would be nice, I won't even lie. I've been looking online and in the Java API for hints and cannot find it. Anyway, my code is below for the first part, but the advanced part, I cannot figure out!

 
/*
 * Challenge 2: Re-assembler from scratch ;)/>/>
 * Author: 
 */
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;

public class ChallengeP2 {

    public static void main(String[] args) {

        // declaring the stupid and sdf variables.
	Date stupid;
	SimpleDateFormat sdf;
        SimpleDateFormat sdf2;
        SimpleDateFormat sdf3;
        SimpleDateFormat sdf4;
        SimpleDateFormat sdf5;
        SimpleDateFormat sdf6;
        SimpleDateFormat sdf7;
        
        // giving stupid/sdf a value.
	stupid = new Date();
        // "YYYY" = year
	sdf = new SimpleDateFormat("YYYY");
        // "MMMM" = month
        sdf2 = new SimpleDateFormat("MMMM");
        // "EEEE" = day
        sdf3 = new SimpleDateFormat("EEEEE");
        // "h" = hour
        sdf4 = new SimpleDateFormat("h");
        // "mm" = minutes after the :
        sdf5 = new SimpleDateFormat("mm");
        // "ss" = seconds
        sdf6 = new SimpleDateFormat("ss");
        // "SSS" = milliseconds
        sdf7 = new SimpleDateFormat("SSS");
        
        
        // shows the current date in a showMessageDialog window.
        // threw in a line break to make it look better and get some practice using it.
       JOptionPane.showMessageDialog(null, "The current year is:\n"+sdf.format(stupid));
       JOptionPane.showMessageDialog(null, "The current month is:\n"+sdf2.format(stupid));
       JOptionPane.showMessageDialog(null, "The current day is:\n"+sdf3.format(stupid));
       JOptionPane.showMessageDialog(null, "The current hour is:\n"+sdf4.format(stupid));
       JOptionPane.showMessageDialog(null, "The current minutes are:\n"+sdf5.format(stupid));
       JOptionPane.showMessageDialog(null, "The current seconds are:\n"+sdf6.format(stupid));
       JOptionPane.showMessageDialog(null, "The current milliseconds are:\n"+sdf7.format(stupid));
   
       // curly brace ends the public
       
       }
       // this curly brace ends the public class.
}

This post has been edited by macosxnerd101: 21 January 2013 - 03:21 PM
Reason for edit:: Please use a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Format Dates Using SimpleDateFormat

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3524
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

Re: Format Dates Using SimpleDateFormat

Posted 21 January 2013 - 03:57 PM

I don't think you can have searched too long as there are examples all over the place; for example.

BTW If you plan on handing your above code to your tutor I would change the name 'stupid': I doubt that it will give a good impression.
Was This Post Helpful? 0
  • +
  • -

#3 bobjob5  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 21-January 13

Re: Format Dates Using SimpleDateFormat

Posted 21 January 2013 - 04:08 PM

Actually, the challenge was given to us with stupid.

It's supposed to be a guy who doesn't know what he's doing, so I left the variable the same since I really don't know what I'm doing.

=)
Was This Post Helpful? 0
  • +
  • -

#4 bobjob5  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 21-January 13

Re: Format Dates Using SimpleDateFormat

Posted 21 January 2013 - 05:21 PM

I've tried a bunch but I don't think that's what I'm looking for.

He wants us to create the same as above but in one SimpleDateFormat.

I can't find it anywhere in that example. Could be because they're using different solutions. I might be having problems translating that in to code that I've learned. I don't want to submit something we haven't quite learned yet.
Was This Post Helpful? 0
  • +
  • -

#5 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Format Dates Using SimpleDateFormat

Posted 21 January 2013 - 05:43 PM

Investigate calenders and the method getCalendar in the SimpleDateFormat class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1