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
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);
How to convert the following string to date or calendar object in Java?
You can use SimpleDateFormat#parse()
to convert a String
in a date format pattern to a Date
.
String string = "2011-03-09T03:02:10.823Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
Date date = new SimpleDateFormat(pattern).parse(string);
System.out.println(date); // Wed Mar 09 03:02:10 BOT 2011
For an overview of all pattern characters, read the introductory text of SimpleDateFormat
javadoc.
To convert it further to Calendar
, just use Calendar#setTime()
.
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// ...
Converting a Date object to a calendar object
Here's your method:
public static Calendar toCalendar(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
Everything else you are doing is both wrong and unnecessary.
BTW, Java Naming conventions suggest that method names start with a lower case letter, so it should be: dateToCalendar
or toCalendar
(as shown).
OK, let's milk your code, shall we?
DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
date = (Date)formatter.parse(date.toString());
DateFormat
is used to convert Strings to Dates (parse()
) or Dates to Strings (format()
). You are using it to parse the String representation of a Date back to a Date. This can't be right, can it?
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.
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 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
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
Related Topics
Efficient Swapping of Elements of an Array in Java
Is Java.Util.Date Using Timezone
What Is the Cross-Platform Way of Obtaining the Path to the Local Application Data Directory
Hiding Instance Variables of a Class
Jsf Convertdatetime Renders the Previous Day
List.Clear() VS List = New Arraylist<Integer>();
How to Add an Image to a Jbutton
How to Change a Field Name in JSON Using Jackson
Spring Boot @Responsebody Doesn't Serialize Entity Id
Why Business Logic Should Be Moved Out of Jsp
Calendar Returns Date in Wrong Time Zone
Retrieve Version from Maven Pom.Xml in Code
Synchronizing on an Integer Value