How to apply applyPattern for SimpleDateFormat in java? - java

I am trying to add a new pattern to the date display but I am not getting the result that I am expecting:
Here is my code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
sdf.applyPattern("yyyy-MM-dd");
Date date_out = null;
try {
date_out = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((TextView) view.findViewById(R.id.date)).setText(date_out.toString());
I want the output to look something like this: 03 Oct 2011
However this is the output tat I am getting: Oct 03 00:00:00 GMT+ 11:00 2011
How do I reach the desired output?
EDIT:
I solved this code by adding this line:
sdf.format(date_out);
instead of setText()

Date.toString(); does always format your String that way. You should a SimpleDateFormat to format the Date object to the String you want.
The JavaDoc of the Date.toString(); method says:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy

You have to use two SimpleDateFormat objects. One for parsing the input and an other one for formatting the parsed Date object to String.
final String inputDate = "2011-05-08T11:12:13.0123";
final SimpleDateFormat inputParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
Date date_out = null;
try {
date_out = inputParser.parse(inputDate);
} catch (final ParseException e) {
e.printStackTrace();
}
final SimpleDateFormat outputFormatter =
new SimpleDateFormat("dd MMM yyyy", Locale.US);
final String result = outputFormatter.format(date_out);
System.out.println(result);
Output:
08 May 2011

The Date.toString() method formats the string like that (check the api documentation).
You do not actually need to use applyFormat(...) in this case. You want to parse one format and output it in another format.
To parse the date (given the string: 2011-10-03") use can use the format"yyyy-MM-dd"and when you output theDateyou want to use"dd MMM yyyy"`:
public static void main(String[] args) throws ParseException {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy MMM dd");
Date parsedDate = inputFormat.parse("2011-10-05");
System.out.println(outputFormat.format(parsedDate));
}
Outputs (on US locale):
2011 Oct 05

Read below document :-
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
hope help u above link.

Related

comparing two date objects of different formats

I have two dates.
The first date is the system time. The second date is related to a news article and when the article expires, it is called end_time.
Im using selenium to test that the article does in fact expire when the system time exceed the the end_time.
My code is as follows:
String searchstring = poriginal;
//make objects to be compared
Date parsed_system_time=null;
Date parsed_end_time=null;
//generate a current time object
GenerateSimpleTime current_time = new GenerateSimpleTime();
current_time.setSystem_time_snapshot();
String system_time = current_time.getSystem_time_snapshot();
//set up the SimpleDateFormat to be used for parsing the strings into objects for comparison
//parsing the date format e.g : 04:11:2016 11:34 AM
SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yyyy hh:mm");
try{
System.out.println("Trying to parse system time: \n");
parsed_system_time = sdf.parse(system_time);
}
catch(ParseException e)
{
System.out.println("Couldnt parse system time...\n");
e.printStackTrace();
}
SimpleDateFormat end_time_sdf = new SimpleDateFormat("dd MMMM, yyyy hh:mm a");
try {
parsed_end_time = end_time_sdf.parse(end_date);
} catch (ParseException e) {
System.out.println("Couldnt parse end_date...\n");
e.printStackTrace();
}
while(parsed_system_time.before(parsed_end_time))
{
current_time.setSystem_time_snapshot();
try {
system_time = current_time.getSystem_time_snapshot();
parsed_system_time = sdf.parse(system_time);
System.out.println("endtime is: "+ parsed_end_time+"\n");
} catch (ParseException e) {
System.out.println("Couldnt parse current_time.getSystem_time_snapshot()...\n");
e.printStackTrace();
}
//System.out.println("system time is: \n");
}
When i run the program the dates are in the following format
endtime: Fri Nov 04 13:49:00 AEST 2016
systemtime: 04:11:2016 1:52 PM
if it a problem when comparing the two dates if they are in a different format. It shouldn't matter right?
When I run the test my program goes and runs indefinitely and doesnt detect when system time is greater than the end time.
The setSystem_time_snapshot() does the following:
String pattern= "dd:MM:YYY h:mm a";
SimpleDateFormat simpletime = new SimpleDateFormat(pattern);
system_time_snapshot = simpletime.format(new Date());
System.out.println("system time snapshop is "+system_time_snapshot+"\n");
Any ideas where I clean up this mess and get it working properly?
So your setSystem_time_snapshot() is returning a string in the format of
dd:MM:YYY h:mm a
But your sdf is
SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yyyy hh:mm");
If you endtime is: Fri Nov 04 13:49:00 AEST 2016,
you should use "EEE MMM dd HH:mm:ss zzzz yyyy" in your SimpleDateFormat
SimpleDateFormat end_time_sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");

SimpleDateFormat - Unparseable?

Take a look at my code:
try {
// String date = "30Jul2013";
SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
Date d = sdf.parse(date);
SimpleDateFormat nsdf = new SimpleDateFormat("MMMM dd, yyyy h:mm a", Locale.ENGLISH);
String nd = nsdf.format(d);
System.out.println(nd);
return nd;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Im am getting a error:
java.text.ParseException: Unparseable date: "2013-07-30 10:58:55.171"
at java.text.DateFormat.parse(DateFormat.java:337)
I would like to have an output of July 30, 2013 11:10 AM from the simpledateformat. There's LOCALE in my code. So what else should I do?
Thanks in advance!
try {
// String date = "30Jul2013";
SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
Date d = sdf.parse(date);
Your date String variable line is commented out, so who's to know what String you're parsing? -- the JVM that's who.
As Robert Harvey points out, the String that you're actually trying to parse is printed for you in the exception message. If you print that String before you parse you'll also see that it's not what you expect it is and that the compiler's right.
In sum, you are somehow expecting that your sdf SimpleDateFormat object is formatting a String of a format similar to "30Jul2013", but the JVM is telling you that this simply is not so, that the String you are trying to parse in fact looks nothing like this, but rather is "2013-07-30 10:58:55.171".

How to change the date format in Ireport 4.5

I have given text field expression new.java.util.Date() and pattern MMMMM dd, yyyy as the mentioned format.
The date must display like: jan 13, 2012 but it's displaying in some other format: Fri Jan 13 08:30:12 IST 2012.
So how to print the date in the mentioned format. And one thing in preview the date displays correctly as mentioned but inside my application it displays Fri Jan 13 08:30:12 IST 2012 format. Is there any way to make it to work properly?
new SimpleDateFormat("MMM dd, yyyy ").format(new Date())
Put the above line in text field so you will get your Date format
Use this below method..hope it will help to you
public static String getDateTimeForUgcServer(String date)
{
SimpleDateFormat intputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = new Date();
try
{
dt = intputFormat.parse(date);
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String str = outputFormat.format(dt);
return str;
}

Java DATE Parsing

I´m having a stupid problem with java.util.Date.
I have this line of code, but I don´t understand why this date is unparseable with this format.
public class TestTime {
public static void main(String[] args) {
final String DATE_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy";
String date = "Sat Dec 31 10:00:00 CET 2011";
SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT);
Date lDate = null;
try {
lDate = dFormat.parse(date);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
If your system uses a locale other than English you need to use this constructor:
SimpleDateFormat(DATE_FORMAT,Locale.ENGLISH);
If this is not the problem, you should format a date using the same formatter and compare the output to your input string.
I don't see anything wrong with this. It executes for me without error, and returns:
Sat Dec 31 09:00:00 GMT 2011
Seems to be a Locale-related problem.
If I set a French locale, the pattern does not work. If I set the Locale to be US in the SimpleDateFormat constructor, it does works.
SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);

String (dd-MM-yyyy HH:mm) to Date (yyyy-MM-dd HH:mm) | Java

I have a string in "dd-MM-yyyy HH:mm" and need to convert it to a date object in the format
"yyyy-MM-dd HH:mm".
Below is the code I'm using to convert
oldScheduledDate = "16-05-2011 02:00:00";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldDate = (Date)formatter.parse(oldScheduledDate);
Now when I print oldDate, i get
Sat Nov 01 02:00:00 GMT 21, which is completely wrong, what am I doing wrong here?
String dateSample = "10-01-2010 21:10:05";
String oldFormat = "dd-MM-yyyy HH:mm:ss";
String newFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
try {
System.out.println(sdf2.format(sdf1.parse(dateSample)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
"yyyy-MM-dd" doesn't even look the same as "16-05-2011". Hmm. Well, why not?
Hints:
DateFormat is very literal. It takes the format specified and uses it -- nothing fancy.
Process: Input Date String -> Convert(With Input Format) -> Date -> Convert(With Output Format) -> Output Date String
The code in the post contains an Output Format, but no Import Format.
A simple approach is to swap around the letters.
String s = "16-05-2011 02:00:00";
String newDate=s.substring(6,10)+s.substring(3,6)+'-'+s.substring(0,2)+s.substring(10);
you need to use formatter when you want to output formatted date
public static void main(String[] args) throws ParseException {
String oldScheduledDate = "16-05-2011 02:00:00";
DateFormat oldFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldDate = (Date)oldFormatter .parse(oldScheduledDate);
System.out.println(formatter.format(oldDate));
}

Categories

Resources