Unexpected Output while trying yo convert String to date using FastDateFormat - java

Problem:
I need to pass Date class Object to a function and that Date Object should contain one Day ahead of the System Time.
For Ex:
If Today's Date is 2017-04-20 17:01:31.Then,Date Object should contain 2017-04-21 17:01:31
Is it possible to store a specified format into Date Class Object and pass into it.
I tried the following thing and it didn't work.
Can anyone guide me if it is possible through Code or should I use SQL Query Concept to add a Day.
Below is my Code
public static void main(String[] args) throws ParseException {
String dateFormat="yyyy-MM-dd HH:mm:ss";
String s2=addDate(dateFormat);
convertStringToDate(s2,dateFormat);
}
public static Date convertStringToDate(String dateInStr, String dateFormat) throws ParseException
{
FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);//("yyyy-MM-dd HH:mm:ss");
Date date = null;
date = fdf.parse(dateInStr);
System.out.println("From convertStringToDate ");
System.out.println(date);
return date;
}
public static String addDate(String dateFormat) throws ParseException{
FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE,1);
String s1=fdf.format(c.getTime());
System.out.println("From addDate ");
System.out.println(s1);
return s1;
}
Expected Output from convertStringToDate:
2017-04-21 17:01:31
OutputShown from convertStringToDate:
Fri Apr 21 17:01:31 IST 2017

You need to obtain the instance of class Date. In your method addDate you get the required Date and then convert it to String and return a String. And then you convert your String back to Date Why don't you just return Date from your method addDate and be done with it?

Related

Unable to parse date YYYY-MM-DDThh:mm:ssTZD from string using DateUtils.parseDate

Having date as string: "2021-09-11T12:02:50-06:00Z".
Want to convert to java.util.Date using apache DateUtils:
public static Date toDate (String dateString) throws ParseException {
String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'TZD''Z'";
return DateUtils.parseDate(dateString, new String[]{DATETIME_FORMAT});
}
giving below exception:
java.text.ParseException: Unable to parse the date: 2021-09-11T12:02:50-06:00Z
at org.apache.commons.lang3.time.DateUtils.parseDateWithLeniency(DateUtils.java:388)
at org.apache.commons.lang3.time.DateUtils.parseDate(DateUtils.java:302)
at org.apache.commons.lang3.time.DateUtils.parseDate(DateUtils.java:279)
tried DATETIME_FORMAT as "yyyy-MM-dd'T'HH:mm:ss'TZD''Z'" , "yyyy-MM-dd'T'HH:mm:ss'TZD''Z'", "YYYY-MM-DD'T'hh:mm:ss'TZD'"
Please make this correction in format and also in date string.
public static Date toDate (String dateString) throws ParseException {
String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
return DateUtils.parseDate(dateString, new String[]{DATETIME_FORMAT});
}
And in input format do not use colon.
Example
System.out.println(toDate ("2021-09-11T12:02:50-0600"));

Parsing dates not giving the same value in the other way

So this is the simple code for what i would like to see happening (returning same values):
public class Main {
public static void main(String[] args) {
org.joda.time.format.DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MM-yyyy");
LocalDate date = LocalDate.now();
String dateAsString = date.toString(dtf);
System.out.println("Date a String: "+dateAsString);
System.out.println("String a Date: "+dtf.parseLocalDate(dateAsString));
}
}
And the output is
Date a String: 28-01-2018
String a Date: 2018-01-28
How should I do this parsing to get the same date format in both directions?
Using joda time library.
Edit: how should i recover the value from the String with that format?

Java SimpleDateFormat.parse throws : Unparseable date: "2015-10-06T08:00:00+00:00" (at offset 10)

Hello i have the following string with datetime:
public static String nextOccurenceString = "2015-10-06T08:00:00+00:00";
And i want to parse and format string into the following format by following pattern:
public static String pattern = "yyyy-MM-dd HH:mm:ss";
But when i try to call method which should parse the date string into date object i always get exception:
Unparseable date: "2015-10-06T08:00:00+00:00" (at offset 10)
Method is following:
public static void convertStringToDate(String dateString) {
try {
SimpleDateFormat sdf;
sdf = new SimpleDateFormat(pattern, Locale.ENGLISH);
Date test = sdf.parse(nextOccurenceString);
Logger.d(test.toString());
} catch (Exception e) {
Logger.e(e.getMessage());
}
}
And I'm using the standard formatting and parsing class:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
How can i solve it please? Should replace something in nextOccurenceString or can i work with string in format like:
"2015-10-06T08:00:00+00:00" ?
Many thanks for any advice.
Your pattern is wrong. It must be:
public static String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
For more information read the javadoc of SimpleDateFormat
The correct pattern for your string is yyyy-MM-dd'T'HH:mm:ssZ (ISO 8601) and not yyyy-MM-dd HH:mm:ss

Converting string to java.util.date

I am trying to convert string in java.util.date but I am having following errors:
HelloWorld.java:10: error: incompatible types: Date cannot be converted to String
return FORMATTER.parse(date);
^
HelloWorld.java:16: error: incompatible types: String cannot be converted to Date
Date date = convertStringToDate("2015-08-03 09:19:00.000");
My code is below:
import java.text.SimpleDateFormat;
import java.util.Date;
public class HelloWorld{
private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public static String convertStringToDate(String date) {
if(date!=null){
return FORMATTER.parse(date);
}
return null;
}
public static void main(String []args){
Date date = convertStringToDate("2015-08-03 09:19:00.000");
System.out.println(date);
}
}
Change your method signature into this instead:
public static Date convertStringToDate(String date)
SimpleDateFormat.parse returns a Date, not a String.
Also, you need to handle the checked ParseException that the parse method may throw, either by declaring throws ParseException in the signature (and handling the exception in main), or by wrapping the exception into a RuntimeException (effectively terminating the program when bad input is given):
public static Date convertStringToDate(String date) {
if (date != null) {
try {
return FORMATTER.parse(date);
} catch (ParseException e) {
// nothing we can do if the input is invalid
throw new RuntimeException(e);
}
}
return null;
}
Finally, you should notice that you can only parse hour values ranging from 1 to 12 with your current format (yyyy-MM-dd hh:mm:ss). If you'd want to parse according to the 24-hour clock, you should use the HH pattern for the hour part instead:
private static final DateFormat FORMATTER = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Your specified format is not matching with data format.
format "yyyy-MM-dd hh:mm:ss" is NOT compatible with data "2015-08-03 09:19:00.000" because of two reason: (1) AM/PM is missing in date as 'hh' takes 1-12 hrs , and (2) milliseconds present in date string
Replace below mentioned line and your issue will be resolved.
private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
But, none of the above mentioned issue can cause the exception you got as milliseconds is ignored by default.
Possible reasons of exception are return type of method "convertStringToDate" or hh having value beyond the range between01 and 12.
change return type from String to Date
Change date format hh to HH.
It is because you are trying to return a String from your method. Change it to Date.
public static Date convertStringToDate(String date) {
if(date!=null){
return FORMATTER.parse(date);
}
return null;
}

parse exception while converting string with timezone to date

I am trying to parse a string with a time zone into a date format, how can I accomplish this?
I am using the code below, but I am getting a parse exception. What I am trying to do is getting the date in UTC time zone, irrespecitve of the time zone I get in the input string. Below is the code:
public class Test1
{
public static void main(String[] args) throws ParseException
{
// TODO Auto-generated method stub
String DIA_TIME1="201307111611400";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmssz");
Date testdate=sdf1.parse(DIA_TIME1);
System.out.println("Current System Time is: " + testdate);
}
}
You have both String date and format, for your date try:
String DIA_TIME1="20130711161140-0000";
and for the string format:
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmssZ");
the last one is upper case Z.
Or, if ois not your intention to have a time zone, you have a spare 0 at the end.
Try this (your date does not contain timezone, it seems that you have milliseconds):
new SimpleDateFormat("yyyyMMddHHmmssS");

Categories