I am trying to SimpleDateFormat date in java - java

I am trying to format a date by parsing it and then formating it but it is not working.
It is showing a parsing exception
public java.util.Date convertFormat(String DateTimeForm)
throws ParseException {
DateTimeForm="2012-06-01 10:00 PM";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
java.util.Date FCDate = (java.util.Date) formatter.parse(DateTimeForm);
return (java.util.Date) FCDate;
}

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
try {
Date date = formatter.parse("2012-06-01 10:00 PM");
System.out.println(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
Didn't change anything and yet it works.
Fri Jun 01 22:00:00 CDT 2012

This works fine on my machine. I didn't change anything important.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
Date date = null;
try {
date = formatter.parse("2012-06-01 10:00 PM");
} catch (ParseException ex) {
// Intentionally empty. Failed parse causes date == null.
}
System.out.print(date);
prints
Fri Jun 01 22:00:00 EDT 2012
The Java docs say the numerics are all locale-independent, but not the AM/PM. For example the code fails if you specify Locale.JAPAN in the formatter construction. Specify Local.US to guarantee AM/PM will always work.

Related

Convert from string to UTC format

I am developing an Android app.
In this app I am working with dates.
I need all dates to be in UTC format. I am using this method to convert them:
public Date getConvertedUTCDate(String datetime) {
Date myDate = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
try {
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
myDate = dateFormat.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
}
return myDate;
}
But instead of getting the expected:
Sun Aug 10 14:13:14 UTC 2014
I get it in CEST format
Sun Aug 10 14:13:14 CEST 2014
What am I doing wrong?
You only set the TimeZone for the calendar of the parser. Not the TimeZone of the parsed result.

How to convert date for the first timezone to the second?

There is the following info:
date ("2013-08-30 07:05:25")
timezone ("Europe/Moscow")
My app gets this info from some server, and I need to convert this date/time to current user's timezone. I know that I should use DateFormat and TimeZone APIs, but I don't understand how. Please, give me a piece of advice or some code. Thanks.
Below code will work for you & refer this stack question for timezones-in-java :
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
cal.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date resultdate = new Date(cal.getTimeInMillis());
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
System.out.println("String date:"+sdf.format(resultdate));
try {
System.out.println("Date:"+sdf2.parse(sdf.format(resultdate)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OUTPUT
String date:2013-08-31 06:25:54 PM
Date:Sat Aug 31 18:25:54 IST 2013

Date formatting issue in java

When I parsing time in java, I passing "12:12" as string argument, then I am getting "Thu Jan 01 12:12:00 IST 1970" as a output.
I want current year like "Fri Mar 09 12:12:00 IST 2012" as a output.
String timestr = "12:12";
Date convertedDate = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
convertedDate = dateFormat.parse(timestr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(convertedDate);
Thanks!
I think that the problem with this is that you are creating a date with null values and then just initialize the time value. I think you should use the Calendar class and get an instance of the Calendar and then set the time. Once that is done, you create a date object from the Calendar and parse it to your needs.

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;
}

SimpleDateFormat.parse()

SimpleDateFormat.parse() accepts the date 003/1/2011 when the format is MM/dd/yyyy. Trying with code below:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date dt2;
try
{
dt2 = sdf.parse(_datemmddyyyy);
}
catch (ParseException e)
{
return false;
}
and the date is parsed as 00/11/2011. What is wrong?
Are you sure? This:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date dt2 = sdf.parse("003/1/2011");
System.out.println(dt2);
Yields:
Tue Mar 01 00:00:00 PST 2011
That's 03/01/2001 in MM/dd/yyyy. Seems right?

Categories

Resources