Convert String to Calendar Object in Java
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
note: set Locale
according to your environment/requirement
See Also
- Javadoc
converting String to Calendar. What is the easiest way?
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(stringInstanceRepresentingDate));
changing string to Calendar object value.
You should use a date format to parse your string
String dateStr = "2014-09-11";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
DateTime date = format.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
How to convert a String to a Calendar object?
Change your data model to use a Date
. This is the usual type to be stored in the database.
You can set the Date
to a Calendar
by using
Calendar c = Calendar.getInstance();
c.setTime(date);
To retrieve the Date
from a Calendar
you can use
date = c.getTime();
Using a String
to store a Date
in a database needs formatting and parsing of the String
s and also no comparision iside the database can be done.
How to convert a date String to a Date or Calendar object?
In brief:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
try {
Date date = formatter.parse("01/29/02");
} catch (ParseException e) {
e.printStackTrace();
}
See SimpleDateFormat
javadoc for more.
And to turn it into a Calendar
, do:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
I wanted to convert this string into calendar object of the specific date, but all it does is giving me the current date
I just ran this code and works fine for me. startDate.getTime()
returns Tue Aug 01 15:18:01 PDT 2017
, which is as expected.
Only thing is you're missing a semicolon at the end of line startDateDate = dateFormat.parse(startDateStr)
.
This might also help you: Set the Calendar to a specific date
how to convert the a string to a calendar object in java
Use Calendar cal = Calendar.getInstance()
to get a calendar object with the current date and time. Using the add(), get(), and set() methods, you can set the calendar object correctly. For instance, to change the date to tomorrow's, you could do: cal.add(Calendar.DAY_OF_MONTH, 1);
To set the hour, cal.set(Calendar.HOUR, hr);
where hr was initialized with the hour to be set. Similarly for minutes, etc.
How to place a date-time string into a Calendar object?
String dateString=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("2012-08-02 12:04:03");
Calender cal = Calender.getInstance();
cal.setTime(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(dateString).getTime());
wrap it around try catch though as the parse method would throw ParseException. and study the link provided by @user714965
Spring: Convert String from View to a Calendar Object
I'm assuming your Project
class has the field DeadLineDate
(fields should start with a lowercase character).
Annotate it with @DateTimeFormat
like so
@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want
private Calendar DeadLineDate;
Your client will then need to send the appropriate pattern.
Related Topics
Dbcp - Validationquery for Different Databases
Httpurlconnection Timeout Settings
Should I Close the Servlet Outputstream
Httpurlconnection Invalid Http Method: Patch
Convert Base64 String to Image
How to Find the Jvm Version from a Program
Java.Io.Ioexception: Mark/Reset Not Supported
Httpservletrequest - How to Obtain the Referring Url
Method to Dynamically Load Java Class Files
Java 8 Instant.Now() with Nanosecond Resolution
"Loading Class Com.Mysql.Jdbc.Driver ... Is Deprecated" Message