format date using SimpleDateFormat - java

I am getting parser Exception while parsing the below date using simple date format API.
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
java.text.SimpleDateFormat dateformate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.parse(inputTimeStamp);
Please help me out on this.

Change
java.text.SimpleDateFormat dateformate=
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
to
java.text.SimpleDateFormat dateformate=
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
You have slashes (/) in your inputTimeStamp

Because you are parsing a date which is in a different format then you have described it to the SimpleDateFormat. If effDate is of type String and should hold the formatted date, the following code might solve it, although there is not supposed to be an space between GMTand -08:00 .
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
SimpleDateFormat inputDateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss - z");
SimpleDateFormat dateformate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.format(inputDateFormat.parse(inputTimeStamp));
I would highly recommend also joda time, especially when you want to do calculations on the date.
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
DateTimeFormatter inputDateformat = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss - z Z");
DateTimeFormatter dateformate = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.print(inputDateFormat.parseDateTime(inputTimeStamp));

The pattern would be: yyyy/MM/dd HH:mm:ss - z where z is for General Time Zone. SimpleDateFormat.

Try this:
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss - z");
See this FYR

Related

How to convert unix time format to day format month date yeear and time format in java

My time format is coming
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
2108-03-27T17:18:16.985+0530
input Date I have to convert it into other time format
Mar 27,2018 5:18 pm
is expected output can any please suggest me how to convert given time to other time format in java .
SimpleDateFormat sdf = new SimpleDateFormat("MMM d,yyyy h:mm a");
System.out.println(sdf.format(date));
If you have Java 8, use the java.time API:
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.appendPattern("XX")
.toFormatter();
OffsetDateTime odt = OffsetDateTime.parse("2108-03-27T17:18:16.985+0530", parser);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd,yyyy h:mm a", Locale.ENGLISH);
String formattedDate = fmt.format(odt); // Mar 27,2108 5:18 PM
SimpleDateFormat has lots of problems, many of them solved by java.time API, and you should prefer to use those.
For older versions of Java, there's a nice backport, with the same classes and functionality.
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy hh:mm a");
System.out.println(sdf.format(new Date()));
//Mar 27, 2018 06:28 PM
for more
What are the date formats available in SimpleDateFormat class?
You can convert type any-to-any of date format.
You just need to pass String format to SimpleDateFormate.
Use this method as static and call it from anywhere by passing inputDate
Simplified Date convert method:
public static String getFormattedDate(String inputDate)
{
String outputFormattedDate = "";
try
{
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z");
Date inputFormatDate = inputFormat.parse(inputDate);
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd,yyyy h:mm a");
outputFormattedDate = outputFormat.format(inputFormatDate);
}
catch (Exception ex)
{
outputFormattedDate = inputDate;
ex.printStackTrace();
}
return outputFormattedDate;
}
Hope it will help you.

Formating Date from parameter in BIRT

Hi so my report takes in a startDate string parameter with the value of "2017/03/18" being passed in. I need to take that and format it to be 18 March 2017.
I've tried using formatter.format(params["startDate"].value,'dd MMM yyy'); but it still doesn't work. I've also used the Format DateTime in the properties tab and it also doesn't work.
Any ideas on how I can convert it to the format I want to?
Thank you!
First You need to convert string date to Date object and then convert the date to String in the format you need.
//Before Java 8
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("dd MMMM yyyy");
String startDate = "2017/03/18";
Date sDate = sdf.parse(startDate);
String convertedDate = sdf1.format(sDate);
// Using Java 8 LocalDate & DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd MMMM yyyy");
LocalDate ldateTime = LocalDate.parse(startDate,formatter);
String formattedDate = ldateTime.format(formatter1);

Java Unparseable Date Exception

I am trying to replace the hyphens with a forward slash, but it results to an unparseable date exception
String test = "2014-04-01 05:00:00";
Date date = new SimpleDateFormat("YYYY/MM/dd hh:mm:ss", Locale.ENGLISH).parse(test);
System.out.println(date);
I have the necessary values for it to be converted, can someone tell me why it returns an error? Plus I wanted to append an am/pm marker at the end of the format, is this possible?
You need to parse String to Date first in correct format as input String
yyyy-MM-dd HH:mm:ss
then you can use format() to print it in other format
yyyy/MM/dd hh:mm:ss
and don't expect toString() method of Date class return formatted value, it is fixed implementation
From SimpleDateFormat:
Letter Date or Time Component <br />
y Year <br />
Y Week year
H Hour in day (0-23)
h Hour in am/pm (1-12)
So, use yyyy for year and HH for hours in the day. Also, you're separating the fields by -, not by /:
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(test);
After doing this, as #JigarJoshi suspects, you can format your Date to look with another format:
String dateInDesiredFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a", Locale.ENGLISH).format(date);
Or written as a complete block of code:
DateFormat parse = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH);
DateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a", Locale.ENGLISH);
String test = "2014-04-01 05:00:00";
Date date = parse.parse(test);
System.out.println(format.format(date));
Produces the following output:
2014/04/01 05:00:00 AM
String test = "2014-04-01 05:00:00";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date oldDate = formatter.parse(test);
formatter.applyPattern("yyyy/MM/dd HH:mm:ss a");
Date newDate = formatter.parse(formatter.format(oldDate));
System.out.println(formatter.format(newDate));

How to convert 'dd/mm/yyyy hh:mm:ss am/pm' to hh:mm:ss (without am or pm) in java?

I want the convert the string like 1/15/2014 9:57:03 AM or 1/15/2014 5:49:39 PM to 9:57:03 and 17:49:39
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.US);
Date date = format.parse("1/15/2014 9:57:03 AM");
format = new SimpleDateFormat("HH:mm:ss");
String dateString = format.format(date);
BTW, use Google next time
You should see this in the doc
doc SimpleDateFormat

How to convert date and time stored in string to 24 format time in android?

I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format

Categories