Convert SimpleDateFormat to GregorianCalendar

Y

yelledbett

I'm somewhat of a newbie to java so bare with me if this is an obvious
issue.

I have 2 SimpleDateFormat objects.

yyyy-MM-dd && HH:mm:ss are the format the SDF objects are in.

I want to create a GregorianCalendar off of these objects.

To create the GregorianCalendar I need these individual variables in
the int format (I think).

Other than putting the SimpleDateFormat into a String and using
StringTokenizer.. can anyone think of another way?

Thanks!
Sonny
 
H

Heiner Kücker

(e-mail address removed)
I'm somewhat of a newbie to java so bare with me if this is an obvious
issue.

I have 2 SimpleDateFormat objects.

yyyy-MM-dd && HH:mm:ss are the format the SDF objects are in.

I want to create a GregorianCalendar off of these objects.

To create the GregorianCalendar I need these individual variables in
the int format (I think).

Other than putting the SimpleDateFormat into a String and using
StringTokenizer.. can anyone think of another way?

Thanks!
Sonny

Set the time as long value:

GregorianCalendar cal = new GregorianCalendar();
cal.setTime( yourDate.getTime() );

Read this:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html#setTime(java.util.Date)


Heiner Kuecker
Internet: http://www.heinerkuecker.de http://www.heiner-kuecker.de
JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
Java Expression Formula Parser: http://www.heinerkuecker.de/Expression.html
CnC Template Technology http://www.heinerkuecker.de/Expression.html#templ
Domain Specific Languages http://www.heinerkuecker.de/DomainParser.html
 
Y

yelledbett

Thanks for the reply.. I guess I didn't explain this very well..

my 2 SimpleDateFormats are being read from the DB.

String datepattern = "yyyy-MM-dd";
SimpleDateFormat data_date = new SimpleDateFormat(datepattern);
data_date.applyPattern(string_date);

String timepattern = "HH:mm:ss";
SimpleDateFormat data_time = new SimpleDateFormat(timepattern);
data_time.applyPattern(string_time);

Basically I have 2 different choices the way I see it.

1.) Tokenize the string_date and string_time and create the Calendar.
2.) Somehow convert SimpleDateFormats to ints and create the Calendar.

Thanks in advance!
Sonny
 
R

Roland

Thanks for the reply.. I guess I didn't explain this very well..

my 2 SimpleDateFormats are being read from the DB.

String datepattern = "yyyy-MM-dd";
SimpleDateFormat data_date = new SimpleDateFormat(datepattern);
data_date.applyPattern(string_date);

String timepattern = "HH:mm:ss";
SimpleDateFormat data_time = new SimpleDateFormat(timepattern);
data_time.applyPattern(string_time);

Basically I have 2 different choices the way I see it.

1.) Tokenize the string_date and string_time and create the Calendar.
2.) Somehow convert SimpleDateFormats to ints and create the Calendar.

Thanks in advance!
Sonny
OK, you get two values out of a database.
One is a date value in "yyyy-MM-dd" format, for example "2004-08-21"
One is a time value in "HH:mm:ss" format, for example "22:12:30".
That's what I understand from your description, but tell me if this is
incorrect.

I'm not sure what you are trying to accomplish next.

Do you want these two values together to represent one date&time value?
In that case I would create a combined SimpleDateFormat, like this:
------------------------------
String combinedPattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat datetimeParser = new SimpleDateFormat(combinedPattern);

String string_date = ... retrieved from DB;
String string_time = ... retrieved from DB;
String string_datetime = string_date + " " + string_time;

java.util.Date dateAndTime = null;
try {
dateAndTime = datetimeParser.parse(string_datetime);
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
------------------------------


Now, you can use the java.util.Date instance 'dateAndTime' for the
things you want to do, e.g.
------------------------------
System.out.println(dateAndTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateAndTime);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
------------------------------

HTH,
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
J

John C. Bollinger

Thanks for the reply.. I guess I didn't explain this very well..

my 2 SimpleDateFormats are being read from the DB.

String datepattern = "yyyy-MM-dd";
SimpleDateFormat data_date = new SimpleDateFormat(datepattern);
data_date.applyPattern(string_date);

String timepattern = "HH:mm:ss";
SimpleDateFormat data_time = new SimpleDateFormat(timepattern);
data_time.applyPattern(string_time);

Basically I have 2 different choices the way I see it.

1.) Tokenize the string_date and string_time and create the Calendar.
2.) Somehow convert SimpleDateFormats to ints and create the Calendar.

No, you have only one choice: figure out what it is that you _really_
want to do. The idea of creating a Calendar from DateFormats is
nonsensical. Calendars are for performing date arithmetic; DateFormats
are for performing date formatting and parsing. Calendars have much
more in common with Dates than they do with DateFormats, and there is no
way to create an instance of the former from instances of the latter.

Before you can figure out what you really want to do, you need to first
figure out what you are in fact doing already. It is unlikely that you
are reading SimpleDateFormat objects from a DB, and certainly the
example code you provided doesn't appear to do that. Are you reading
the format _patterns_ from a DB? That would be odd, but certainly more
likely, and it might explain the mysterious "string_time" and
"string_date" variables in your code. More likely yet, however, is that
you are reading strings representing dates and times from your DB, and
you want to do something with them.

So what is it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top