Formating Date from parameter in BIRT - java

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

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.

Illegal Argument Exception for setDate in java

I have the following code which converts the Java LocalDate to a particular format while passing as a parameter for setDate.
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
String formattedString = localDate.format(formatter);
storedProcedureCall.setDate(1, java.sql.Date.valueOf(formattedString));
But it always throws an IllegalArgumentException when I include formattedString in the argument for setDate but works fine when I do:
storedProcedureCall.setDate(2, java.sql.Date.valueOf(java.time.LocalDate.now()));
In general:
storedProcedureCall.setDate(2, java.sql.Date.valueOf(java.time.LocalDate.now())); // working
storedProcedureCall.setDate(2, java.sql.Date.valueOf(formattedString)); // not working
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
String formattedString = localDate.format(formatter);
LocalDateTime ldt = LocalDateTime.from(LocalDate.parse(formattedString, formatter).atStartOfDay());
storedProcedureCall.setDate(1, Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()));
This should work. But if you work with standard library that use java.util.Date but not new classes like LocalDate, it is more comfortable to use old java classes.
As I understand, your goal is to set current date to storedProcedureCall. In Java any date is a long. Do not worry about local and other features, until you convert it to String. So you could solve your problem with only one line:
storedProcedureCall.setDate(1, new java.util.Date());
P.S. How to convert 24 oct 2017 to Date instance and set it to storedProcedureCall:
storedProcedureCall.setDate(1, new SimpleDateFormat("dd MMM yyyy", Locale.US).parse("24 oct 2017"));
Use this format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Your Code should be like that:
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedString = localDate.format(formatter);
storedProcedureCall.setDate(1, java.sql.Date.valueOf(formattedString));

format date using SimpleDateFormat

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

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

How can I convert a String date with full month names to a Date object in java?

How can I convert dates with full month names into a Date object in Java? Here's a sample date that I need to convert: 6 December 2002.
Thanks.
String str = "6 December 2002";
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
Date date = (Date)formatter.parse(str);
Here is working IDE One demo
Must See
API Doc
Have a look at SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("d MMMMM yyyy", Locale.US);
Date d = sdf.parse("6 December 2002");
System.out.println(d);

Categories