Can any one tell me solution for the following:
I have a Util.Date object in java.I want to validate the date entered. I am parsing the date object using the required format.
For ex, Format is "MM/dd/yyyy" and Date entered is "23/12/2010"
For this date, I am not getting any Parse Exception but the date is adjusted to "11/12/2011" which should not happen in my case.Instead I want to throw error message.
Please help me in this asap.
Thanks in advance
In DateFormat class you have to reset "lenient" flag, i.e.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
. . .
sdf.parse(mydate, pos);
You must set your "lenient" mode of the SimpleDateFormat to false:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
By default it is tolerant to some errors, and tries to interpret them somehow.
Depending upon your format expression , we can adjust month, date and year using SimpleDateFormat , code is below
public class SimpleDateFormatString2DateParse {
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
try {
Date date = formatter.parse("11/12/2011");
System.out.println("Date is : " + date);
} catch (Exception e) {
e.printStackTrace();
}
}
}
-----output-----
Date is : Sat Nov 12 00:00:00 PST 2011
Related
This question already has answers here:
Date Format JAVA
(5 answers)
Closed 9 years ago.
I have a date in the following format
//input date
Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)
//output date format
I want to change this to "dd-mm-yyyy, hh:mm:ss".
I get the input date format from db. I have to change that into output date format which i will be showing it in a grid.
I tried the following code.
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
try
{
Date date = outputDate.parse(facade.getDate.toString()); **//getting exception here**
outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
Date date1 = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").parse(outputDate
.format(date));
facade.setDate(date1);
}catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
I am getting
java.text.ParseException: Unparseable date: "2013-06-06 00:00:00.0".
Any help..
"2013-06-06 00:00:00.0" does not match "dd-mm-yyyy, hh:mm:ss" your format should be "dd-MM-yyyy hh:mm:ss" instead
But, looking at your code I'm guessing facade.getDate is actually a java.sql.Timestamp which inherits from java.util.Date so you can directly pass it to the format like so
new SimpleDateFormat("dd-MM-yyyy, hh:mm:ss").format(facade.getDate)
Here's some code which works for me:
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
String input = "Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)";
DateFormat inputFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z",
Locale.ENGLISH);
Date date = inputFormat.parse(input);
DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",
Locale.ENGLISH);
outputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String output = outputFormat.format(date);
System.out.println(output);
}
}
Things to consider:
You need to work out your output time zone. Currently I've got it set to UTC, but that may not be what you want.
You really need to take a step back and think things through. You've clearly got two different formats - you're trying to convert from one to the other. So creating three different SimpleDateFormat objects all with the same format is never going to work.
You need to read documentation carefully... in SimpleDateFormat, M means month and m means minute; h uses the 12-hour clock and H uses the 24-hour clock.
This is assuming you actually need to start with a string though. If getDate is already a Date or a Timestamp, you can ignore the first part - just use the output part of the above code. You should avoid unnecessary string conversions wherever possible.
Note that dd-MM-yyyy is a slightly unusual format - are you sure you don't actually want yyyy-MM-dd which is more common (and sortable)?
DateFormat outputDate = new SimpleDateFormat("yyyy-dd-mm hh:mm:ss");
try {
Date date = outputDate.parse("2013-06-06 00:00:00.0");
System.out.println(new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
works well, line 1 was incorrect. Your SimpleDateFormat.parse needs to be in the exact format of the input date. Then you want to output it in a different format so you make another one and set the format then call SimpleDateFormat.format(date) and I put a println on it.
Fault is here
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
pattern should be equals to Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time). not to your out put strings pattern.
#Test
public void test() throws ParseException {
SimpleDateFormat sdf_org = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
Date d = sdf_org.parse("Thu Jun 06 2013 00:00:00 GMT+0530");
SimpleDateFormat sdf_target = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");
System.out.println(sdf_target.format(d));
}
output console : 2013-30-06 03:30:00.000
i am getting my date input as a string in the format dd-mm-yy through jsp page. Because this format is better to understand . But now i want to store the date in yyyy-MM-dd HH:mm:ss format in my database. I am using the following code.
try
{
String s="30-04-2013";
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
d1=new SimpleDateFormat("yyyy-MM-dd").parse(s);
System.out.println(d1);
System.out.println(ft.format(d1));
} catch(Exception e) {
System.out.println("Exception:"+e);
}
My Jsp date format is dd-mm-yy, but it gives answer as
Tue Oct 04 00:00:00 IST 35
0035-10-04 00:00:00
what is my mistake?
can tell me anyone please
Thank you.
d1=new SimpleDateFormat("yyyy-MM-dd").parse(s);
should be
d1=new SimpleDateFormat("dd-MM-yyyy").parse(s);
because the input date String you've provided String s="30-04-2013"; is of the format, dd-MM-yyyy. Hence, to parse this, you need to give dd-MM-yyyy format in your SDF.
Change the format in your code from yyyy-MM-dd to dd-MM-yyyy. A simple debug would have solved this issue.
i think you need to pass the Date object to format() method of SimpleDateFormat class instead of passing string to it
just try to do like as follows
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatted_date = ft.format(new Date());
System.out.println(formatted_date);
let me know the status
happy coding
See below the working program. See the parse and format method.
import java.text.SimpleDateFormat;
public class DateParsing {
public static void main(String args[]) {
String s = "30-04-2013";
SimpleDateFormat ft = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat nt = new SimpleDateFormat("yyyy-MM-dd");
try {
System.out.println(nt.format(ft.parse(s)));
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
I have the input string as 2012-07-27 and I want the output as Date but with the same format like 2012-07-27
Code
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
Date today = df.parse("20-12-2005 23:59:59");
System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
Output
20-12-2005 23:59:59
But it's string object I want the same output (20-12-2005 23:59:59) as date object not as string object.
How can I get the Date in the form DD-MM-YYYY HH:MM:SS?
Date today is the date object you get for input String. There are nothing like formatted dates in Java. Date is always just date object. You perform all sorts of operations on that date object and when you want to Store (or) display just apply format()
df.format(today) // is just for formatting and display purpose.
There is difference in your format passed to SimpleDateFormat and way you are passing date string. You should Also use HH
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
try {
Date today = df.parse("20-12-2005 23:59:59");
System.out.println("Today = " + df.format(today));
//To Print Real Today
System.out.println("Real Today = " + df.format(new Date()));
} catch (ParseException e) {
e.printStackTrace();
}
The Date class has many deprecated methods and the only correct way to create it right now is via a long (read doc for details).
You should look into GregorialCalendar where you can pass some constant fields of Calendar as attributes.
If you want to input the date from your String, I would either do a custom parser that creates a calendar or something like this.
Hope I helped :)
I 'm getting the date in the following format (Date and not String):
Tue Jun 26 07:00:00 EDT 2012
I want to change the format of the date to (Date):
6/26/2012 10:19:15 AM
so as to update the same in the data base. I tried following code:
Date dte;
Date dte1;(Tue Jun 26 07:00:00 EDT 2012)
SimpleDateFormat formatter = new SimpleDateFormat("m/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte1);
dte = formatter.parse(formattedDate);
SystemUtils.trace("test", " date>>>" + dte);
is yielding the following response:
Thu Jan 26 07:00:00 EST 2012
Can any one please share the piece of code to do the same asap.
You shouldn't have to format dates to insert them in a database. If using JDBC, use prepared statements.
To answer your question, though, m can't mean minute and month at the same time. M means month. m means minute.
This code outputs needed for you result:
Date dte = new Date();//or something else
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte);
System.out.println(formattedDate);
Try this, this below code will suit your need.
public class DateWala {
public static void main(String[] args){
System.out.println(new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").format(new Date()));
}
}
you can use this tiny function
// send your time
private String convertTime(String dateTime) {
//source format will go there
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Date date = null;
try {
date = sdfSource.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
//destination format will go there
SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy HH:mm");
return sdfDestination.format(date);
}
I'm having a hard time Parsing/Formatting a Date string received back from a web service. I've attempted multiple approaches, but with no luck.
Sample Date String:
2011-10-05T03:00:00Z
Exception:
W/System.err(10072): java.text.ParseException: Unparseable date: "2011-10-05T05:00:00Z" (at offset 10)
W/System.err(10072): at java.text.DateFormat.parse(DateFormat.java:626)
Sample Code:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSSS");
Date date = formatter.parse(info.AiringTime);
I've found that if I remove the "T" between the date and the time and replace it with a space, it will format just fine. Anybody have any suggestions?
--UPDATE--
After looking deeper into the API documentation, I found this:
All response DateTime values are in UTC format. You need to apply the UTC offset to calculate the local time for display.
DateTime is a date-and-time value specified in one of the following formats:
UTC format: YYYY-MM-DDThh:mm:ssZ. For example: 2011-03-15T02:00:00Z.
Local time with an offset: YYYY-MM-DDThh:mm:ss + or - hh:mm (positive or negative offset). For example, for US Pacific time: 2011-03-14T06:00:00 -08:00.
Any suggestions on the UTC format approach?
You could try:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = dateString.replace("Z", "GMT+00:00");
Date date = dateFormat.parse(dateString);
The above code should correctly handle the case where a timezone is specified in the date. As Z represents the UTC/GMT timezone it is replaced by GMT so the SimpleDateFormat can interpret it correctly (i would love to know a cleaner way of handling this bit if anyone knows one).
Try,
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
This pattern should parse the date you provide: "yyyy-MM-dd'T'HH:mm:ss'Z'".
If you want to use SimpleDateFormat and you have a limited number of variations, you can create separate formatters for each pattern and chain them:
Date date = formatter1.parse(info.AiringTime);
if (date == null)
{
date = formatter2.parse(info.AiringTime);
if (date == null)
{
date = formatter2.parse(info.AiringTime);
if (date == null)
{
date = formatter3.parse(info.AiringTime);
}
}
}
or put them in a list and iterate until non-null or no more formatters.
If you have too many patterns for this to be practical, you can parse it yourself or try one of these libraries.
This worked for me
SimpleDateFormat isoDateFormat = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss'Z'");
SimpleDateFormat viewFriendlyDateFormat = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss aaa");
String viewFriendlyDate = "";
try {
Date date = isoDateFormat.parse(timestamp);
viewFriendlyDate = viewFriendlyDateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat isoDateFormat = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss'Z'");
SimpleDateFormat viewFriendlyDateFormat = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss aaa");
String viewFriendlyDate = "";
try {
Date date = isoDateFormat.parse(timestamp);
viewFriendlyDate = viewFriendlyDateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}