There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Java - Problem with Formatting Date


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
r3drock3t88's Avatar
Computer Specs
Senior Member with 231 posts.
 
Join Date: Jan 2007
Experience: Advanced
24-Sep-2007, 08:58 PM #1
Java - Problem with Formatting Date
Hey all, what I have to do is have the user input their date in the format "09/22/1988", and I need it to be formatted into the format "Saturday, September 22, 1988", then outputted. This is what I have so far.

// Prompts user for birthday
temp = JOptionPane.showInputDialog("Enter your birthday(mm/dd/yyyy): ");
// Formats the given date
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
temp2 = sdf.format(temp);

JOptionPane.showMessageDialog(null, "You were born on: " + temp);

I tried that out, and got an error because I'm using the .format function with a string. I do not know how to convert my "temp" string that the user inputs into a date, having it formatted, and then have it converted back into a string so I can output it yet again.

Am I using the wrong code? Or can I make this work? Any suggestions are MUCH appreciated ,I've been working on this for a few days now.

Thank you all in advance!
__________________
If you're interested in learning how I make 100% FREE & LEGIT money each month through surveys, check out my personal website below!

Storhino's Personal Blog - Make Money Online!

I wrote an article on how I do it and a picture of my check will be up ASAP... so check it out, it's 100% FREE to participate, and I promise you will receive a personal check when your first months over! .

r3drock3t88
Chicon's Avatar
Computer Specs
Distinguished Member with 6,608 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
25-Sep-2007, 01:53 AM #2
Hi r3drock3t88,

This should solve your problem !

Code:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

public class ShowDate {
    
    public static void main(String[] args) {
       String temp = JOptionPane.showInputDialog("Enter your birthday(mm/dd/yyyy): ");
       // I'm assuming the user's entered date as being valid
       int month = Integer.parseInt(temp.substring(0,2));
       int day = Integer.parseInt(temp.substring(3,5));
       int year = Integer.parseInt(temp.substring(6));
       // I invoke an instance of Calendar in order to get a Date object
       // (Remark : the first month of the year = 0)
       Calendar cal = Calendar.getInstance();
       cal.set(year, month - 1, day);
       Date date = cal.getTime();
       // Transformation of the date
       SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
       JOptionPane.showMessageDialog(null, "You were born on: " + sdf.format(date));       
    }    
    
}
r3drock3t88's Avatar
Computer Specs
Senior Member with 231 posts.
 
Join Date: Jan 2007
Experience: Advanced
25-Sep-2007, 12:24 PM #3
Hi Chicon! Thanks for the quick response, Unfortunately, this is for a class that I am in and we never learned about that just yet. My teacher my get a little fishy as to how i managed that one. I believe the class he wants us to use is java.util.date class. any other suggestions? Thanks again for the help! Its much appreciated
__________________
If you're interested in learning how I make 100% FREE & LEGIT money each month through surveys, check out my personal website below!

Storhino's Personal Blog - Make Money Online!

I wrote an article on how I do it and a picture of my check will be up ASAP... so check it out, it's 100% FREE to participate, and I promise you will receive a personal check when your first months over! .

r3drock3t88
JimmySeal's Avatar
Computer Specs
Junior Member with 6 posts.
 
Join Date: Sep 2007
Experience: Advanced
25-Sep-2007, 12:41 PM #4
Never used these classes, but from looking at the Javadocs, I would think you need to:

Create another instance of SimpleDateFormat for the input format (mm/dd/yyyy).
Date foo = sdf2.parse(temp);
temp2 = sdf.format(foo);
r3drock3t88's Avatar
Computer Specs
Senior Member with 231 posts.
 
Join Date: Jan 2007
Experience: Advanced
25-Sep-2007, 01:41 PM #5
Hmm, the sdf.parse(temp); keeps giving me an error: "Unhandled exception type ParseException" Any idea as to why im getting this error? :-/ What a bummer.
JimmySeal's Avatar
Computer Specs
Junior Member with 6 posts.
 
Join Date: Sep 2007
Experience: Advanced
25-Sep-2007, 02:10 PM #6
parse() throws an exception. You have to contain it in a try{}catch{} structure

try{
Date foo = sdf2.parse(temp);
temp2 = sdf.format(foo);
}
catch(Exception e){
System.err.println("Exception: " + e);
}
r3drock3t88's Avatar
Computer Specs
Senior Member with 231 posts.
 
Join Date: Jan 2007
Experience: Advanced
25-Sep-2007, 03:07 PM #7
Ah, jimmy you're a genius, that got rid of my parse error, but now I get this error when I run the program.

"Exception: java.text.ParseException: Unparseable date: 1988-09-22".

Here is my new code that I am using...

temp = JOptionPane.showInputDialog("Enter your birthday(yyyy-MM-dd): ");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
// Formats the given date
SimpleDateFormat sdf2 = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
try{
Date bday = sdf.parse(temp);
temp2 = sdf2.format(bday);
JOptionPane.showMessageDialog(null, "You were born on: " + temp2);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Exception: " + e);
}

Any ideas why? Thanks again! This is a huge help.
__________________
If you're interested in learning how I make 100% FREE & LEGIT money each month through surveys, check out my personal website below!

Storhino's Personal Blog - Make Money Online!

I wrote an article on how I do it and a picture of my check will be up ASAP... so check it out, it's 100% FREE to participate, and I promise you will receive a personal check when your first months over! .

r3drock3t88
Chicon's Avatar
Computer Specs
Distinguished Member with 6,608 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
25-Sep-2007, 03:43 PM #8
I've tested this one and it works !

Code:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

public class ShowDate {
    
    public static void main(String[] args) {
       String temp = JOptionPane.showInputDialog("Enter your birthday ( mm-dd-yyyy ) : ");

       SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Date date;
        try {
            date = sdf.parse(temp);
            sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
            JOptionPane.showMessageDialog(null, "You were born on " + sdf.format(date));
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
    }    
}
r3drock3t88's Avatar
Computer Specs
Senior Member with 231 posts.
 
Join Date: Jan 2007
Experience: Advanced
25-Sep-2007, 04:17 PM #9
Wow, it's amazing that I forgot one little thing and the entire program didn't run. All I missed was I put commas instead of "-" in the first date format. Thanks a lot ! You all were so much help. Finally I can finish this thing up !!
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:41 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.