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?
Related
String date = "08/02/2022 Tuesday";
DateTimeFormatter LONG_DATE_FORMAT_ddMMyyyyEEEE = ofPattern("dd/MM/yyyy EEEE");
LocalDate.parse(date, LONG_DATE_FORMAT_ddMMyyyyEEEE);
I'm getting a DateTimeParseException with the following message: Text 08/02/2022 Tuesday' could not be parsed at index 11.
I suppose this is an issue with the EEEE side of my format, but I can't seem to understand what should replace it.
This is java 1.8.0_311
We need DateTimeFormatter class to format date string properly. We also need to convert the string date to LocalDate object and back to string again to display. The DateTimeParseException class handles any undesired outcomes.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
class Main {
public static void main(String[] args) {
try{
String date = "08-02-2022 Tuesday";
DateTimeFormatter pattern =
DateTimeFormatter.ofPattern("dd-MM-yyyy eeee");
// parsing string date to LocalDate obj
// The part you were missing
LocalDate formattedDate = LocalDate.parse(date, pattern);
// Again converting to string
System.out.println(formattedDate.format(pattern));
}
// handling exception for unparseble dates
catch(DateTimeParseException x){
System.out.println("The given date cannot be parsed");
}
}
}
LocalDate contains of a day, month, and year (Variation between +999999999-12-31 and -999999999-12-31)
Things like time and other values are rejected by the parsing. If you would like the day of the week, you can use a function like:
// Parses the date
LocalDate dt = LocalDate.parse("2018-11-27");
// Prints the day
System.out.println(dt.getDayOfWeek());
This works for me:
String date = "08/02/2022 Tuesday";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy EEEE");
LocalDate time = LocalDate.parse(date, formatter);
System.out.println(time.format(formatter));
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"));
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?
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 5 years ago.
I am trying to parse string into a date using the following code:
public static Date dateFormatter(String s)
{
SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY");
Date excelDate=null;
try
{
excelDate = ft.parse(s);
Date formatString = ft.format(excelDate);
System.out.println("Date to be printed in Excel is :" +formatString);
return excelDate;
}
catch(Exception ae)
{
System.out.println("No date");
}
return excelDate;
}
I am passing in the argument "04202017".
This function is not working for me. I am not able to figure out what I am doing wrong. Can anybody please help me?
You have to use ft.parse(s); instead of format(excelDate). Format is the other way (Date -> String)
DateFormat.parse(String)
And you dont have to parse the Date back to a String.
Corrected code:
public static Date dateFormatter(String s) {
SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY");
Date excelDate = null;
try {
excelDate = ft.parse(s);
System.out.println("Date to be printed in Excel is :" +excelDate);
return excelDate;
} catch(Exception ae) {
System.out.println("No date");
}
return excelDate;
}
You already parsed String s to excelDate with date format that you want. So i think it's good and enough to print just excelDate.
System.out.println("Date to be printed in Excel is :" +excelDate);
Like that.
And also change MMddYYYY to MMddyyyy.
Try parse method instead of format
For String to Date, use:
SimpleDateFormat.parse(String);
For Date to String, use:
SimpleDateFormat.format(date);
However, in your code, you already parsed the String and assigned into excelDate on this line:
excelDate = ft.parse(s);
try this one:
String string = "march 9, 2017";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
It would be nice to use Java 1.8's new time classes (which are in java.time.* package).
public static void main(String[] args)
{
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// To String
String dateString = dateTime.format(formatter);
System.out.println(dateString);
// To LocalDateTime
LocalDateTime parsedLocalDateTime = LocalDateTime.parse(dateString, formatter);
}
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