Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Has anyone ever used DateFormat.LONG?

dateformat date class

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1 pcSetUp

pcSetUp

    CC Regular

  • Member
  • PipPipPip
  • 44 posts

Posted 22 January 2013 - 02:50 PM

Has anyone ever used DateFormat.LONG?

If you could just give me an example of of a dateFormat I would should be able to figure out my questions below on my own:


Using accessor and mutator methods for each instance variable, as well as an equals method and three output methods to display the date in one of each of these formats:
12/25/2013
December 25, 2013
25 December 2013

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 22 January 2013 - 02:57 PM

If it's just an example you want, have you tried looking at the source for java.text.SimpleDateFormat? SimpleDateFormat extends DateFormat.

Link to source:
http://grepcode.com/...DateFormat.java

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 pcSetUp

pcSetUp

    CC Regular

  • Member
  • PipPipPip
  • 44 posts

Posted 22 January 2013 - 03:21 PM

I am still having trouble with this...Would i be able to input my own date as well?

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 22 January 2013 - 04:21 PM

What exactly do you mean by "input my own date"? DateFormat's format() method takes a Date object, and you can pass any Date object in that you want.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5 Petros

Petros

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 22 January 2013 - 05:47 PM

Show us how much code have you worked so far, even if it don't work as you spect.
Java Java Doooooo!!!

#6 pcSetUp

pcSetUp

    CC Regular

  • Member
  • PipPipPip
  • 44 posts

Posted 22 January 2013 - 07:14 PM

import java.text.DateFormat;
import java.util.Date;
import java.util.Scanner;

/**
*
* @author Home
*/
public class Data {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// Make a new Date object. It will be initialized to the current time.
Scanner keyboard = new Scanner(System.in);
int month, day, year;

// Get Date from user
System.out.print("Enter date (mm/dd/yy): ");
String fullDate = keyboard.next();

month = Integer.parseInt(fullDate.substring(0,fullDate.indexOf("/")));
fullDate = fullDate.substring(fullDate.indexOf("/") + 1);

day = Integer.parseInt(fullDate.substring(0,fullDate.indexOf("/")));
fullDate = fullDate.substring(fullDate.indexOf("/") + 1);

year = Integer.parseInt(fullDate);


// Next, try the default DateFormat
System.out.println(" 2. " + DateFormat.getInstance().format(fullDate));

System.out.println("10. " + DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG).format(fullDate));
}
}

public class Date {


private int month;
private int day;
private int year;


public Date(int month, int day, int year){
this.month = month;
this.day = day;
this.year = year;
}

public void setMonth(int month){
this.month = month;
}

public void setDay(int day){
this.day = day;
}

public void setYear(int year){
this.year = year;
}

public int getMonth(){
return month;
}

public int getDay(){
return day;
}

public int getYear(){
return year;
}

public String displayDate(){
return month + "/" + day + "/" + "/" + year;
}

}
}

#7 pcSetUp

pcSetUp

    CC Regular

  • Member
  • PipPipPip
  • 44 posts

Posted 22 January 2013 - 08:34 PM

Show us how much code have you worked so far, even if it don't work as you spect.



#8 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 23 January 2013 - 06:44 AM

I'm confused. I thought you wanted to use java.text.DateFormat, but you've written your own Date class. java.text.DateFormat must take a java.util.Date object, not a custom written Date object. Unless you plan to extend DateFormat and write a method which takes your Date object. If that's the case, I'd suggest renaming your Date to something slightly different, to avoid ambiguity with java.util.Date.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download