How to convert String date into ISO Date format date [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Is there a way to change dateFormat with java 8 stream?
(4 answers)
Closed 2 years ago.
I have a string date "04/01/2020 at 9:00PM". I would like to convert it into an ISO date format. Looking for optimizing way to convert it into ISO date format

Try this code:
public static void main(String[] args) {
String dateString = "04/01/2020 at 9:00PM";
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy 'at' hh:mma");
try {
Date date = dateFormat.parse(dateString);
System.out.println(date);
} catch(ParseException exception) {
System.out.println(exception.getMessage());
}
}
Checkout this post for more information: https://stackoverflow.com/a/4216767/10030693
UPDATE
Following advise from Mark Rotteveel, DateTimeFormatter is a better API to use to format date in java 8, so this should be preferred:
public static void main(String[] args) {
String dateString = "12/12/2020 at 09:00PM";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("d/M/yyyy 'at' hh:mma", Locale.ENGLISH);
try {
LocalDateTime dateTime = LocalDateTime.parse(dateString, dateTimeFormatter);
System.out.println(dateTime);
} catch (DateTimeException exception) {
System.out.println(exception.getMessage());
}
}

Related

How to convert String e.g. "01/01/2019" to date in Java 8 [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 4 years ago.
I'm trying to use Java 8's DateTimeFormatter to turn strings such as "17/01/2019" into dates of exactly the same format.
I'm currently using:
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.ENGLISH);
LocalDateTime dExpCompletionDate = LocalDateTime.parse(sExpCompletionDate, format);
LocalDateTime dExpCommencementDate = LocalDateTime.parse(sExpCommencementDate, format);
and getting the error:
java.time.format.DateTimeParseException: Text '' could not be parsed at index 0
Which would suggest there's something wrong with my format.
Currently, I've tried using the default format as well as using LocalDate instead of LocalDateTime
You're trying to obtain LocalDateTime instead of LocalDate:
LocalDateTime dExpCompletionDate = LocalDateTime.parse(sExpCompletionDate, format);
Here is a small example with LocalDate:
public static void main(String[] args) {
String sExpCompletionDate = "17/01/2019";
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.ENGLISH);
LocalDate dExpCompletionDate = LocalDate.parse(sExpCompletionDate, format);
// Converts LocalDate into LocalDateTime
LocalDateTime dExpCompletionDate2 = LocalDate.parse(sExpCompletionDate, format).atStartOfDay();
System.out.println(dExpCompletionDate);
System.out.println(dExpCompletionDate2);
}
Output:
2019-01-17
2019-01-17T00:00
Here is an example with LocalDateTime:
public static void main(String[] args) {
String sExpCompletionDate = "17/01/2019 14:22:11";
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH);
LocalDateTime dExpCompletionDate = LocalDateTime.parse(sExpCompletionDate, format);
System.out.println(dExpCompletionDate);
}
Output:
2019-01-17T14:22:11
Because "dd/MM/yyyy" is date pattern, you can't parse to DateTime with it. What you can do is, parse to Date and then get StartOfDay as DateTime
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime dExpCompletionDate = LocalDate.parse("01/01/2019", format).atStartOfDay();
Use SimpleDateFormat. Here is a working example:
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println("Date is : "+date1);
}
}
For a more comprehensive answer, refer to reply by BalusC.

Parsing String into date using java [duplicate]

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

How to format 2016-02-12T15:23:20+02:00 time [duplicate]

This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I'm getting the above Date format from our webservice. I have an idea on how to format the date, im just having issues with the fact it comes down as a string.
I have tried this but I need to return it as a String, which in a way isn't a problem.
This is what I have tried but it throws an Exception:
java.text.ParseException: Unparseable date:
"2016-02-26T00:00:00+02:00" (at offset 4)
Code:
public static String formatDate(String unFormattedTime) {
String formattedTime;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM HH:mm");
Date date = sdf.parse(unFormattedTime);
formattedTime = sdf.format(date);
return formattedTime;
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
How could I format it in a format like dd MMM HH:mm?
Here you are with a working snippet of what you want to achieve:
public class FormatDateExample {
public static void main(String[] args) {
String date = "2016-02-26T00:00:00+02:00";
System.out.println(formatDate(date));
}
public static String formatDate(String unFormattedTime) {
String formattedTime;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = sdf.parse(unFormattedTime);
sdf = new SimpleDateFormat("dd MMM HH:mm");
formattedTime = sdf.format(date);
return formattedTime;
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
}
First you have to parse the date with the given format you have
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = sdf.parse(unFormattedTime);
Then you have to format that date to the desired format "dd MMM HH:mm"
sdf = new SimpleDateFormat("dd MMM HH:mm");
formattedTime = sdf.format(date);
If you are using Java 8 you can try this:
LocalDate parsedDate = LocalDate.parse(unFormattedTime, ISO_OFFSET_DATE_TIME)
Ref:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME

How to convert a given String to a date in java only with the given format? [duplicate]

This question already has answers here:
How to specify which century a date should be parsed in Joda-Time
(2 answers)
Closed 8 years ago.
I have a String in java. I want to convert it into Date. I have used SimpleDateFormat. But the problem is that if I pass dd-mm-yy instead of dd-mm-yyyy, then also it parses the String and converts into Date. Below is the code
private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
public static void main(String args[]){
Date d = null;
String dat="14-01-16 21:59:59";
try {
d = dateFormat.parse(dat);
}
catch (ParseException e){
e.printStackTrace();
}
System.out.println(d.toString());
}
Ideally it show throw exception becuase in the dateFormatter I have given the format as dd-MM-YYYY but in the string I have passed dd-MM-YY format. So is there a way around to allow those Strings only which have only dd-MM-YYYY format only and disallowing the strings which have dd-MM-YY format.
If you don't want to accept YY, only YYYY, use a regex:
private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
public static void main(String args[])
{
Date d = null;
String dat="14-01-16 21:59:59";
try
{
if(!dat.matches("\\d\\d-\\d\\d-\\d\\d\\d\\d.*"))
throw new ParseException(dat, 7);
d = dateFormat.parse(dat);
}
catch (ParseException e)
{
e.printStackTrace();
}
}

How to format date string in java? [duplicate]

This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Android Studio Convert ISO string to "America/New_York" when adding to event to calendar
(1 answer)
Change date format in a Java string
(22 answers)
Closed 9 years ago.
Hi i have the following string:2012-05-20T09:00:00.000Z
and i want to format it to be like 20/05/2012, 9am
How to do so in java?
Thanks
If you are looking for a solution to your particular case, it would be:
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2012-05-20T09:00:00.000Z");
String formattedDate = new SimpleDateFormat("dd/MM/yyyy, Ka").format(date);
use SimpleDateFormat to first parse() String to Date and then format() Date to String
package newpckg;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class StrangeDate {
public static void main(String[] args) {
// string containing date in one format
// String strDate = "2012-05-20T09:00:00.000Z";
String strDate = "2012-05-20T09:00:00.000Z";
try {
// create SimpleDateFormat object with source string date format
SimpleDateFormat sdfSource = new SimpleDateFormat(
"yyyy-MM-dd'T'hh:mm:ss'.000Z'");
// parse the string into Date object
Date date = sdfSource.parse(strDate);
// create SimpleDateFormat object with desired date format
SimpleDateFormat sdfDestination = new SimpleDateFormat(
"dd/MM/yyyy, ha");
// parse the date into another format
strDate = sdfDestination.format(date);
System.out
.println("Date is converted from yyyy-MM-dd'T'hh:mm:ss'.000Z' format to dd/MM/yyyy, ha");
System.out.println("Converted date is : " + strDate.toLowerCase());
} catch (ParseException pe) {
System.out.println("Parse Exception : " + pe);
}
}
}

Categories