Java: Date Format - java

I am trying to parse the following string to date
2013-02-01T09:37:20EST
I need to compare it with current date to see it is before or after current date.
Here is what I am doing
Date formatTime(String time) throws exception
{
String format = "yyyy-MM-dd HH:mm:ss z";
DateFormat dateFormat = new SimpleDateFormat(format);
Date expTime = dateFormat.parse(time);
return expTime;
}
I am getting java.text.ParseException: Unparseable date: "2013-02-01T09:37:20EST" (at offset 10)
Thanks.

notice the space before the timezone z and wrap T around quotes 'T'
String format = "yyyy-MM-dd'T'HH:mm:ss z";

The format should be like this: yyyy-MM-dd'T'HH:mm:ssZ

Your input time String does not match your DateFormat pattern. You could use:
String format = "yyyy-MM-dd'T'HH:mm:ssz";
SimpleDateFormat

Related

Java Date TIME Format AM/PM Configuration

I am facing problems some while formatting the date:
Date : 11/06/2020 04:14:20
Date Format:dd/MM/yyyy hh:mm:ss a
Exception:
java.text.ParseException: Unparseable date: "11/06/2020 04:14:20"
Following is the code
Blockquote
public String getFormatDate(String inputDate) {
String strDate = "";
try {
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss a");
DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date date1 = inputFormat.parse(inputDate);
strDate = outputFormat.format(date1);
}catch( Exception exe) {
exe.printStackTrace();
logger.error( "[ERROR] getFormatDate:. ", exe );
}
return strDate;
}
Blockquote
Any help would be greatly appeciated.
You can check this code you have to pass the am/pm part too with the date string value as your format is expecting that.
//String date = "11/06/2020 04:14:20";
String date = "11/06/2020 04:14:20 am";
DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
https://ideone.com/3nibwJ
Use proper date-time objects for your dates and times
For the vast majority of purposes you should not keep your date and time in a string and should not convert your date and time from a string in one format to a string in another format. Keep your date and time in a ZonedDateTime or LocalDateTime object.
When you are required to accept string input, parse that input into a date-time object immediately. I am using and recommending java.time, the modern Java date and time API:
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
String input = "11/06/2020 04:14:20";
LocalDateTime dateTime = LocalDateTime.parse(input, inputFormatter);
System.out.println(dateTime);
Output so far is:
2020-06-11T04:14:20
Since there is no AM or PM in your string, I have assumed that 04:14:20 was the time of day from 00:00:00 through 23:59:59. If you intended otherwise, you need to explain how.
Only when you need to give string output, format your date and time back into a string of appropriate format:
DateTimeFormatter outputFormatter = DateTimeFormatter
.ofPattern("MMMM dd, yyyy hh:mm:ss a", Locale.ENGLISH);
String output = dateTime.format(outputFormatter);
System.out.println(output);
June 11, 2020 04:14:20 AM
Do provide a locale for the formatter so Java knows which language to use for the month name and the AM/PM indicator.
What went wrong in your code?
Your string has no AM nor PM: 11/06/2020 04:14:20. Yet your format pattern string requires an AM/PM marker in the end. This is what format pattern letter a signifies. So your string wasn’t in the format that you required. This was the reason for the exception that you observed.
Link
Oracle tutorial: Date Time explaining how to use java.time.
Thanks All for your help:
I have changed the source date "11/06/2020 04:14:20" to "06/11/2020 04:14:20 PM", and then after perform follwoing steps, its working for me:
Blockquote
DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
inputFormat.setTimeZone( TimeZone.getTimeZone("UTC") );
Date dDate = inputFormat.parse( srcDate );
String strDeDate = formatDateToString( dDate, "dd/MM/yyyy hh:mm:ss a", "IST" );
public String formatDateToString(Date date, String format,String timeZone) {
if (date == null) return null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
timeZone = Calendar.getInstance().getTimeZone().getID();
}
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
return sdf.format(date);
}
Blockquote

How to convert a string date "2019-04-21T12:08:35" to SimpleDateFormat, there by convert to Date?

For normal date Strings like "2019-04-08 08:35"
SimpleDateFormat df = new SimpleDateFormat("yyyy-dd-mm hh:mm");
but What shall be the shortest conversion for dates like
"2019-04-21T12:08:35"
SimpleDateFormat is not a Date, it's used to
Convert String to Date
Format Date to String
You can parse String to java.time.LocalDateTime directly since java8:
LocalDateTime localDateTime = LocalDateTime.parse("2019-04-21T12:08:35");
System.out.println(localDateTime);
Before Java8 :
SimpleDateFormat df = new SimpleDateFormat("yyyy-dd-yy'T'hh:mm:ss");

change date format DD-MON-YYYY to DD/MM/YYYY

I want to change date format which I received reading from excel cell file is "30-mar-2016" to 03/30/2016. I have tried below
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate;
startDate = df.parse(inputDate);
It gave me- java.text.ParseException: Unparseable date: "30-mar-2016"
.
I also tried below code
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
String startDate;
startDate = df.format(inputDate);
getting -- java.lang.IllegalArgumentException: Cannot format given Object as a Date
Can anyone help me .
Your input string 30-mar-2016 is in the format dd-MMM-yyyy. Your output format is MM/dd/yyyy. So you need two DateForamts. One for parsing original input string, one for formatting output string.
DateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy"); // for parsing input
DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy"); // for formatting output
String inputDate = "30-mar-2016";
Date d = df1.parse(inputDate);
String outputDate = df2.format(d); // => "03/30/2016"
I have resolved using below
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = sdf.parse("30-mar-2016");
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(date));//got 03/30/2016
Thanks everyone for your quick response

How to get the correct format of dateand time in mysql?

When I get the date and time in MySQL it retrieves it in this format:
2016-01-14 14:24:00.0
Where does the .0 come from and how do I get this format with Java:
2016-01-14 14:24:00
You can use 2 ways to do this.
Split the date string at '.'
String date = "2016-01-14 14:24:00.0";
String newDate = date.split("\\.")[0];
System.out.println(newDate);
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf.parse("2016-01-14 14:24:00.0");
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
If you are getting date as string, you can use Format to get the format you need:
SELECT DATE_FORMAT(YourDateField, '%d-%m-%Y %T')
FROM YourTable
Have a look here for all possible formats
If you are getting your date as a java.sql.Timestamp, you can get the corresponding java.util.Date instance very easily and then format it to the desired string representation with a java.text.SimpleDateFormat class.

Convert long timestamp to Java.util.Date in french

I want to know how I can convert a timestamp in the date format.
For example : 1415337782000 should be converted to "7 nov. 2014".
This is what I've tried so far :
Date date=new Date(location.date);
SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
String dateText = df.format(date);
The timestamp is stored in location.date in long type.
Thanks in advance for your help.
You can pass a format to the SimpleDateFormat constructor, like this
SimpleDateFormat format= new SimpleDateFormat("d MMM, yyyy");
String dateText = format.format(new Date());
You can find the constants in this link:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
For better internationalization, you can define a string resource containing the desired format based on the location of the user. Something like
SimpleDateFormat format = new SimpleDateFormat(getResources().getString(R.string.YOUR_FORMAT));

Categories

Resources