SimpleDateFormat.parse examples

Hi,

I am going through below links on SimpleDateFormat.parse examples .

http://examples.javacodegeeks.com/core-java/text/java-dateformat-example///this link used parse() method in their code as below
package com.javacodegeeks.corejava.text;

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

public class DateFormatExample {
	public static void main(String[] args) {

		Date curDate = new Date();

		System.out.println(curDate.toString());

		String DateToStr = DateFormat.getInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateTimeInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.SHORT)
				.format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(
				curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.LONG).format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
				DateFormat.SHORT).format(curDate);
		System.out.println(DateToStr);

		try {
			Date strToDate = DateFormat.getDateInstance()
					.parse("July 17, 1989");
			System.out.println(strToDate.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
}

Open in new window



http://www.xyzws.com/javafaq/how-to-use-simpledateformat-class-formating-parsing-date-and-time/142 // this link did not use parse method in their code as below
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatExample {

    public static void main(String[] args) {
		
        Date now = new Date();
        String datetimeStr = now.toString();
        System.out.println("1. " + datetimeStr);
        SimpleDateFormat format = new SimpleDateFormat(
                    "EEE MMM dd HH:mm:ss zzz yyyy");
        System.out.println("2. " + format.format(now));
    }

}

Open in new window


what is the difference between above two approaches. where is the index position being used here to move forward for next position?
. Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
SimpleDateFormat can be used to either:
a) Parse a string and convert it into a Date object
b) Take a Date object and turn it into a String.

The top one calls the "parse" method on the SimpleDateFormat object:
Date strToDate = DateFormat.getDateInstance()
                              .parse("July 17, 1989");

The bottom one calls the "format" method:
System.out.println("2. " + format.format(now));

That's the difference,

Doug
0
gudii9Author Commented:
package com.javacodegeeks.corejava.text;

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

public class DateFormatExample {
      public static void main(String[] args) {

            Date curDate = new Date();

            System.out.println(curDate.toString());

            String DateToStr = DateFormat.getInstance().format(curDate);
            System.out.println(DateToStr);

            DateToStr = DateFormat.getTimeInstance().format(curDate);
            System.out.println(DateToStr);

            DateToStr = DateFormat.getDateInstance().format(curDate);
            System.out.println(DateToStr);

            DateToStr = DateFormat.getDateTimeInstance().format(curDate);
            System.out.println(DateToStr);

            DateToStr = DateFormat.getTimeInstance(DateFormat.SHORT)
                        .format(curDate);
            System.out.println(DateToStr);

            DateToStr = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(
                        curDate);
            System.out.println(DateToStr);

            DateToStr = DateFormat.getTimeInstance(DateFormat.LONG).format(curDate);
            System.out.println(DateToStr);

            DateToStr = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                        DateFormat.SHORT).format(curDate);
            System.out.println(DateToStr);

            try {
                  Date strToDate = DateFormat.getDateInstance()
                              .parse("July 17, 1989");
                  System.out.println(strToDate.toString());
            } catch (ParseException e) {
                  e.printStackTrace();
            }

      }
}

Bottom one used for below purpose??
b) Take a Date object and turn it into a String.
please advise
0
gudii9Author Commented:
what is difference between format and parse method.
format method basically formats given date object to required string way of represesenting like yyyy/mm/dd etc right.
parse does opposite thing of coverting(yyyy/mm/dd) string to date object(what it means by date object how it looks like??)?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

dpearsonCommented:
what is difference between format and parse method.
format method basically formats given date object to required string way of represesenting like yyyy/mm/dd etc right.
parse does opposite thing of coverting(yyyy/mm/dd) string to date object

Yes that's right.  That's the difference.

(what it means by date object how it looks like??)?
I'm not sure what you're asking here?  Are you asking what a date object is?
Ultimately it's a count of milliseconds since 1st January 1970 - which is very different from the string form.

Doug
0
gudii9Author Commented:
(what it means by date object how it looks like??)?

I'm not sure what you're asking here?  Are you asking what a date object is?
Ultimately it's a count of milliseconds since 1st January 1970 - which is very different from the string form.

 Date strToDate = DateFormat.getDateInstance()
43
                    .parse("July 17, 1989");
44
            System.out.println(strToDate.toString());

Open in new window


gave below outout right

Mon Jul 17 00:00:00 EEST 1989
0
gudii9Author Commented:
Mon Jul 17 00:00:00 EEST 1989
above supposed to be Date object right after doing toString()but it is not seem to me like
it's a count of milliseconds since 1st January 1970 -
please advise
0
gudii9Author Commented:
 DateToStr = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
38
                DateFormat.SHORT).format(curDate);
39
        System.out.println(DateToStr);

Open in new window



what above code does?
Formates date to medium then again to short to print
May 11, 2014 11:37 PM?
i am not clear. please advise
0
dpearsonCommented:
Mon Jul 17 00:00:00 EEST 1989
above supposed to be Date object right after doing toString()but it is not seem to me like
it's a count of milliseconds since 1st January 1970 -

Date strToDate ; // Internally this is represented as a number (milliseconds since 1970).

System.out.println(strToDate.toString()); // When you call "toString()" on a date object it converts it back to a String so it can print it out

So you are going to and from the String form here.

Try this instead:
System.out.println(strToDate.getTime());

It will print out a long number...which is the milliseconds since 1970.  That's the "real" value inside the date object - it's not stored as a string.

Doug
0
gudii9Author Commented:
try {
			Date strToDate = DateFormat.getDateInstance()
					.parse("July 17, 1989");
			System.out.println(strToDate.toString());
System.out.println(strToDate.getTime());
		} catch (ParseException e) {
			e.printStackTrace();

Open in new window


it printed as
616651200000

when i ran
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class DateFormatExample {
	public static void main(String[] args) {

		Date curDate = new Date();

		System.out.println(curDate.toString());

		String DateToStr = DateFormat.getInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateTimeInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.SHORT)
				.format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(
				curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.LONG).format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
				DateFormat.SHORT).format(curDate);
		System.out.println(DateToStr);


		try {
			Date strToDate = DateFormat.getDateInstance()
					.parse("July 17, 1989");
			System.out.println(strToDate.toString());
System.out.println(strToDate.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
}

Open in new window

0
gudii9Author Commented:
which one we use most commonly format or parse?
Please advise.
format() seems straight forward parse bit tricky to me
0
dpearsonCommented:
which one we use most commonly format or parse?
It should be obvious that which one you use depends on the problem you're trying to solve.

You use format when you need to display a date as a string in a specific way (e.g. to print out a date with the day of the week e.g. "Saturday 10/10/2015").

You use parse when somebody else gives you a date as a string and you want to work with it (e.g. figure out the date a week from now when I give you "10/8/2015").

Make sense?

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
there many ways of representing date as Date object and date as String object right. What is the standard way to represent them. Where i can see all different ways of representing it. please advise
0
dpearsonCommented:
What is the standard way to represent them
Inside your program you should use  a Date object.

Outside your program (e.g. on a form that are showing to a user or where a user types in a date) you may use the String form.
0
gudii9Author Commented:
e.g. figure out the date a week from now when I give you "10/8/2015"

can you please elaborate on this. i was not clear?
0
dpearsonCommented:
I'm not sure how to expand on this really.

If I walked up to you and said today is "10/8/2015" and I asked, what is the date a week from now, how would you compute that?

You could try to just work with the String.
E.g. Find the value after the first "/" and before the second "/" : "8"
Add 7 to it: "15"
Merge it back into the string: "10/" + "15" + "/2015" = "10/15/2015"

In this case that would work.  But working with the String is a terrible idea.  What if I give you "12/30/2015".  Now you'd get "12/37/2015".

So instead we convert it to a Date object and work with that.

Make sense?
0
gudii9Author Commented:
yes very clear now.


	 
08
public class SimpleDateFormatExample {
09
    public static void main(String[] args) {
10
 
11
        Date curDate = new Date();
12
 
13
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
14
 
15
        String DateToStr = format.format(curDate);
16
        System.out.println(DateToStr);
17
 
18
        format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
19
        DateToStr = format.format(curDate);
20
        System.out.println(DateToStr);
21
 
22
        format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH);
23
        DateToStr = format.format(curDate);
24
        System.out.println(DateToStr);
25
 
26
        format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
27
        DateToStr = format.format(curDate);
28
        System.out.println(DateToStr);
29
 
30
        try {
31
            Date strToDate = format.parse(DateToStr);
32
            System.out.println(strToDate);
33
        } catch (ParseException e) {
34
            e.printStackTrace();
35
        }
36
    }
37
}

Open in new window


above code is getting date object at the top and displaying in 4 diferent string formats using format() method right
0
gudii9Author Commented:
http://examples.javacodegeeks.com/core-java/text/java-dateformat-example

in above link what is difference between  section 1 example (http://examples.javacodegeeks.com/core-java/text/java-dateformat-example) and section 2(2. Example of DateFormat)??
please advise
0
gudii9Author Commented:
both places they named DateToStr  as variable name that is confusing.

Looks like second section author is trying to manipulate the date object itself using date object related methods right.

where as first section for given date object it is converting to various other string representations for display right?
0
gudii9Author Commented:
In this example we will show how to use java.text.SimpleDateFormat class so as to format date into text or parse text into date. SimpleDateFormat extends the java.text.DateFormat class which is an abstract class for date/time formatting subclasses and provides many class methods for obtaining default date/time formatters based on any given locale. We will see the usage of the class DateFormat, too.


looks like DateFomrat is super class of SimpleDateFormat  which is abstract class.

They gave example on both.

What is locale author is talking about as below

java.text.DateFormat class which is an abstract class for date/time formatting subclasses and provides many class methods for obtaining default date/time formatters based on any given locale.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.