Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Format Dates Using SimpleDateFormat

$
0
0
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.
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>