How to Convert the Following string into Date Object? - java

I am New to Android Development.How to convert the Following string(5/31/2011) in to the Date Object I tried different ways But Couldn't get any Luck??

String dateStr = "5/31/2011";
DateFormat df = new SimpleDateFormat("M/dd/yyyy");
Date date = df.parse(dateStr);

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
try {
Date today = df.parse("5/31/2011");
System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}

You can use the following
String time1 = "5/31/2011";
SimpleDateFormat formatter1 = new SimpleDateFormat("MM/dd/yyyy");
Date indate1 = formatter1.parse(time1);
Thanks Deepak

Related

How to convert date into gmt in java

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format("2017-01-04"));
The above is my code and when I try to convert the date to gmt I get the error:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot
format given Object as a Date at
java.text.DateFormat.format(DateFormat.java:310) at
java.text.Format.format(Format.java:157) at
Sample.main(Sample.java:24)
Please point out what am I doing wrong.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format(new Date()));
This above code is working fine but I need something that works for any date.
And when I try this code:
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.parse("2017-01-04"));
} catch(Exception e) {
e.printStackTrace();
}
I am getting Unparseable date: "2017-01-04". Please tell me how to fix it
Try it..
String s = "2016-11-21 13:30:0.0";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss);
Date d = null;
try
{
d = format.parse(s);
} catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(gmtFormat.format(d));
When you have a date object then you can format it as you want:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
//create a calendar representing your date.
Calendar cal = Calendar.getInstance();
cal.set(2017, 01, 04);
//format the date object to the pattern you want.
String DateToStr = format.format(cal.getTime());
System.out.println(DateToStr);
If you have your date as string you can also do:
String string = "2017-01-04";
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
format1.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
try {
date = format1.parse(string);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println(date);
The below code is to help in understanding better - you can try uncommenting the commented lines and compare the results. The point of interest for you in this whole block is gdf.parse(gmtFormat)
Date date = new Date();
DateFormat idf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
idf.setTimeZone(TimeZone.getTimeZone("IST"));
DateFormat gdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
gdf.setTimeZone(TimeZone.getTimeZone("GMT"));
//String istFormat = idf.format(date);
//System.out.println("IST: "+istFormat);
String gmtFormat = gdf.format(date);
System.out.println("GMT: "+ gmtFormat);
try {
//System.out.println("gmt parsed from istFormat: "+gdf.parse(istFormat));
//System.out.println("ist parsed from istFormat: "+idf.parse(istFormat));
System.out.println("gmt parsed from gmtFormat: "+gdf.parse(gmtFormat));
System.out.println("ist parsed from gmtFormat: "+idf.parse(gmtFormat));
} catch (ParseException e) {
e.printStackTrace();
}

How to convert String into DateFormat in java?

I am working on Java. I have a String and I want to convert it into Date Format. Please see the lines of code for more details.
String dateString = "4:16:06 PM";
I want to convert it into the below Date:
Date convertedDate = 2014-04-22 16:16:06.00
How can I do this?
try this:
// get current date
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
String a = dateFormat1.format(new Date());
// add current date to your time string
String dateString = a + " 4:16:06 PM";
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd h:mm:ss a");
try {
// parse it. This is you date object.
Date d = dateFormat2.parse(dateString);
} catch (ParseException ex) {
ex.printStackTrace();
}
I think you are looking for following:
DateFormat df1 = new SimpleDateFormat("h:mm:ss a");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS");
String dateString = "4:16:06 PM";
Date date;
try {
Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
date = df1.parse(dateString);
cal.setTime(date);
// Reset year, month, day to current state
cal.set(Calendar.YEAR, cal2.get(Calendar.YEAR));
cal.set(Calendar.MONTH, cal2.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, cal2.get(Calendar.DAY_OF_MONTH));
Date convertedDate = cal.getTime();
System.out.println(df2.format(convertedDate));
} catch (ParseException e) {
e.printStackTrace();
}
Try this ,
String str_date="13-09-2011";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MM-yyyy");
date = (Date)formatter.parse(str_date);
System.out.println("Date " +date.getTime());
Try this
public class DateClass
{
public static void main(String[] args) throws Exception {
String target = "4:16:06 PM";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dff = new SimpleDateFormat("HH:mm:ss");
Date d=new Date();
String abc="4:16:06 PM";
Date result = new SimpleDateFormat("h:mm:ss a").parse(abc);
System.out.println("The result is "+df.format(d)+" "+dff.format(result));
}
}
Feel free to ask anything if you don't understand it and plz respond did it work or not?

I want to parse String into java date object [duplicate]

This question already has answers here:
convert string into date format
(5 answers)
Closed 9 years ago.
Hello i have date in this format 2013-10-31T19:00:00Z now i want to display this date into yyyy/mm/dd this format.i have used this code but its giving me exception kindly help me
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Try this
String DateStr="2013-10-31T19:00:00Z";
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
Date d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
System.out.println(sim.format(d));
output 2013-10-31
That's because you're specifically telling Java to look for a 4-digit year followed by a 2-digit month and 2-digit date. Taken straight from the docs, what you want is
"yyyy-MM-dd'T'HH:mm:ssZ"
You can use SimpleDateFormat's parse method to parse your date, and then use the format method to output it in the format that you prefer.
Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = inputFormat.parse(startDateString);
System.out.println("Date is " + outputFormat.format(date));
Output:
Date is 2013/10/31
You need a separate SimpleDateFormat to parse the Datestring. Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat originalFormat = new SimpleDateFormat("yyyy-mm-dd'T'HH:MM:SS'Z'");
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = originalFormat.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is " + newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
You need two different DateFormat. one for parse date and one for formatting date.
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS'Z'");
Date startDate;
try {
startDate =df1.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Here is simple example
Try this:
try {
String startDateString = "2013-10-31T19:00:00Z";
DateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat output = new SimpleDateFormat("yyyy/MM/dd");
Date date = input.parse(startDateString);
System.out.println("Date is " + output.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Pointing an another simple mistake in your code
You coded as DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
here mm is for minutes , for month use MM

convert string to date and format the date

I have String variable which contains date. Now how to display July 15 from that string variable? (for example 2012-07-15)
String strdate = "2012-07-15";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd");
try {
d1 = formatter.parse(strdate);
System.out.println(d1 + "---date first---");
} catch (ParseException e) {
e.printStackTrace();
}
Your parsing template does not match your example:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
(with MM instead of MMM).
To output July 15, you can use a second formatter:
SimpleDateFormat output = new SimpleDateFormat("MMMM dd");
System.out.println(output.format(d1)); //July 15
Something like,
try {
String strdate = "2012-07-15";
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"");
DateFormat df2 = new SimpleDateFormat("MMMM dd");
System.out.println(df2.format(df1.parse(strdate)+ "---date first---");
}
catch (ParseException e) {
// Exception handling
}

Date convert dd-MMM-yyyy to dd-MM-yyyy in java

What is the simplest way to convert 23-Mar-2011 to 23-03-2011 in Java?
Thanks everyone. This seem to solve the issue:
try {
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("dd-MMM-yyyy").parse("24-Nov-2002"));
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(cal.getTime()));
} catch (ParseException e) { e.printStackTrace(); }
Try this
String strDate="23-Mar-2011";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date varDate=dateFormat.parse(strDate);
dateFormat=new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Date :"+dateFormat.format(varDate));
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Have you looked at SimpleDateFormat ?
Here's a simpler version of your code:
DateFormat shortFormat = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH);
DateFormat mediumFormat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
String s = "23-Mar-2011";
String shortDate = shortFormat.format(mediumFormat.parse(s));
System.out.println(shortDate);
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("24-Nov-2002");
System.out.println(format2.format(date));

Categories