0

I have the following data in a text file which later I will Scanner in.

17-Mar-2006 1100 1280
16-Jan-2002 1120 1230
15-Jun-2003 1140 1900

I'm building this data as an "Stockmarket" object. The instance variable "date" is declared as GregorianCalendar. The constructor, String dt, should expect the form "dd-MMM-yyyy" in String. Same goes to the public String date(), it should return a string of the form"dd-MMM-yyyy". I done it in the following way, but outputs: The date is java.util.GregorianCalendar[time=1141556400000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=".....

Any idea on a fix?

public class Stockmarket{
    // instance variables
    private GregorianCalendar date; 
    private double opening;
    private double closing; 

    public Stockmarket(String dt, double opening, double closing)

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
    Date d = sdf.parse(dt);
    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    cal.setTime(d);
    this.date=cal;
    this.opening=opening;
    this.closing=closing;
    }  

    public String date(){

    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy, Locale.US");
    df.setCalendar(this.date);
    String date = df.format(this.date.getTime());
    return date;
    }
Katherine
  • 257
  • 2
  • 6
  • 15
  • 1
    Are you sure you want to use GregorianCalendar for date storing? – Laurentiu L. May 25 '15 at 08:49
  • 1
    first result on google "String to GregorianCalendar" -> http://stackoverflow.com/questions/240510/convert-a-string-to-gregoriancalendar – Sharon Ben Asher May 25 '15 at 08:49
  • @LaurentiuL. The professor set it that way, I have no choice :) – Katherine May 25 '15 at 08:52
  • @Katherine You may question his ways, even if it's for a homework. Most professors will accept a non-standard response provided it's intriguing enough or well thought. – Laurentiu L. May 25 '15 at 08:54
  • @LaurentiuL. We did... and he asked us to look at the API and figure out a way... – Katherine May 25 '15 at 08:55
  • @Katherine And what is stoping you to look at the API? You posted another question which holds all the relevant string to date processing utilities and Gregorian Calendar's documentation is pretty straight forward http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html – Laurentiu L. May 25 '15 at 08:57
  • You have a typo ... `new SimpleDateFormat("dd-MMM-yyyy, Locale.US")` should be `new SimpleDateFormat("dd-MMM-yyyy", Locale.US)` – Kenneth Clark May 25 '15 at 09:57
  • @KennethClark I'm still looking for the output fix :) – Katherine May 25 '15 at 10:06
  • What is the issue ? It looks like you are just calling the toString() on the GregorianCalendar which produces `java.util.GregorianCalendar[time=1142546400000......` take a look at my answer below , `getStringFromDate` will return the date formatted; i cant tell from the above where the issue is. – Kenneth Clark May 25 '15 at 10:08
  • i dont understand. the question was how to convert string to gregorian calendar. why is it something totally doifferent now ?!? – Sharon Ben Asher May 25 '15 at 10:10
  • @KennethClark sorry I don't understand, the output I'm looking is a date in the form of "dd-MMM-yyyy", did i miss something? Can you please advice – Katherine May 25 '15 at 10:10
  • @sharonbn yes it was, but even after the conversion, the output is not what i'm looking for at all.... – Katherine May 25 '15 at 10:13
  • Im half expecting becuse you have a method called date and a calendar instance called date you are doing something like `System.out.println(date);` <- witch is the gregorian cal instance and not `System.out.println(date());` <- which is your method – Kenneth Clark May 25 '15 at 10:13
  • 1
    @KennethClark yes i think you are right, thanks Ken!! really appreciate it ! :) – Katherine May 25 '15 at 10:22
  • @Katherine, if the question changes during some conversation that you have with one or more user, please do not change the title. it is very confusing for other who are trying to help. you can open a new question or continue with the conversation w/o chaniging the title – Sharon Ben Asher May 25 '15 at 10:25

1 Answers1

2

You can achieve what you want with SimpleDateFormat , if the format is known and uniformed.

public class TestParse {
    public static void main(String[] args) {
        String myDate = "17-Mar-2006";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
        Date date = null;
        try {
            date = simpleDateFormat.parse(myDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar calendar =  new GregorianCalendar();
        calendar.setTime(date);
        System.out.println(calendar.getTime());
    }
}

EDIT: Added more information based on question change

the method getStringFromDate produces a formatted date of 17-Mar-2006

public class TestParse {
    public static void main(String[] args) {
        String myDate = "17-Mar-2006";
        Date date = getDateFromString(myDate);
        // Set the Calendar
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // format date
        String formattedDate = getStringFromDate(calendar);
        System.out.printf("The formatted date is : %s & the dates are equal %s%n", formattedDate, formattedDate.equals(myDate));
    }

    public static String getStringFromDate(GregorianCalendar calendar) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
        simpleDateFormat.setCalendar(calendar);
        return simpleDateFormat.format(calendar.getTime());
    }

    public static Date getDateFromString(String input) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
        Date date = null;
        try {
            date = simpleDateFormat.parse(input);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

Your Stockmarket class Fixed type on format

    public class Stockmarket {
        // instance variables
        private GregorianCalendar date;
        private double opening;
        private double closing;

        public Stockmarket(String dt, double opening, double closing) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
            Date d = null;
            try {
                d = sdf.parse(dt);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
            cal.setTime(d);
            this.date = cal;
            this.opening = opening;
            this.closing = closing;
        }

        public String date() {
            SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
            df.setCalendar(this.date);
            String date = df.format(this.date.getTime());
            return date;
        }
    }

Testing Class

public class TestParse {

    public void doTest()
    {
        Stockmarket stockmarket1 = new Stockmarket("17-Mar-2006",500,600);
        stockmarket1.date();
        System.out.println(stockmarket1.date());

        Stockmarket stockmarket2 = new Stockmarket("16-Jan-2002",500,600);
        stockmarket2.date();
        System.out.println(stockmarket2.date());

        Stockmarket stockmarket3 = new Stockmarket("15-Jun-2003",500,600);
        stockmarket3.date();
        System.out.println(stockmarket3.date());

    }
    public static void main(String[] args) {

        TestParse testParse = new TestParse();
        testParse.doTest();
    }
}
Kenneth Clark
  • 1,725
  • 2
  • 14
  • 26