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);
}
}
}
Related
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());
}
}
This question already has answers here:
Why is Java's SimpleDateFormat not thread-safe? [duplicate]
(9 answers)
Closed 4 years ago.
I am converting Date to string format in yyyy-MM-dd HH:mm:ss format to save in sqlite database
below is object declared for simple date format
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sometime it prepends pair of zeros in date as you can see in below image
Example Wrong date returns as as below
2018-08-25 02:32:0000
2018-08-25 02:32:0049
2018-0008-25 02:32:50
2018-08-24 0023:32:50
2018-08-0024 23:32:50
I have created custom funtion to correct this wrong date. But I want to know exact cause of this issue.
below is the code
public static String getCurrentDateTime() {
Date d = new Date();
String datetime = sdf.format(d);
if (datetime.length() > 19) {
datetime = correctDate(datetime);
}
return datetime;
}
I'm sure that if you don't use that static instance of SimpleDateFormat you will have no problem:
public static String getCurrentDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
String datetime = sdf.format(d);
return datetime;
}
See these links:
Why is Java's SimpleDateFormat not thread-safe?
"Java DateFormat is not threadsafe" what does this leads to?
SimpleDateFormat is not thread safe that's why you should check if in your calling code there is no issue with that.
This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 4 years ago.
I have the date format as "dd/MM/yyyy", i would like to convert this into MM DD, yyy an example:
10/10/1996 -> Oct 10, 1996
Also the other way around case:
Oct 10, 1996 --> 10/10/1996
I have been trying for 1+ hour but couldnt figure it out as i have never used a date formatter class, if anyone could help me it would be awesome, PL: java
Im assuming your'e using DateTime.
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
String strDate = sdfDate.format(now);
This code will format a DateTime to string as 2018-04-07.
You can play around with the format as you want. search for list of the format keys (such as MM, as I mentioned here).
//Use these imports
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
Use this code to convert your date format from one to another:
String fromDateFormat = "dd/MM/yyyy";
String fromdate = 15/03/2018; //Take any date
String CheckFormat = "dd MMM yyyy";//take another format like dd/MMM/yyyy
String dateStringFrom;
Date DF = new Date();
try
{
//DateFormatdf = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat FromDF = new SimpleDateFormat(fromDateFormat);
FromDF.setLenient(false); // this is important!
Date FromDate = FromDF.parse(fromdate);
dateStringFrom = new
SimpleDateFormat(CheckFormat).format(FromDate);
DateFormat FromDF1 = new SimpleDateFormat(CheckFormat);
DF=FromDF1.parse(dateStringFrom);
System.out.println(dateStringFrom);
}
catch(Exception ex)
{
System.out.println("Date error");
}
Note: Change your SimpleDateFormat accordingly.
This question already has answers here:
How to convert "2017-04-26T20:55:00.000Z" string to a Date in java android studio [duplicate]
(3 answers)
Closed 5 years ago.
I am calling t a web service to retrieve dates. These dates come in this format
"2018-01-24T05:00:00.000Z"
Now, I need to parse that to more legible, something like "dd-MM-YYYY", e.g. 1-24-2018.
How can I do it?
I'm using Android Studio and a read about Instant but it requires minSdkVersion 26 and is set to 15 what is perfect for me right now.
You can use custom deserializer for JSON for dates.
You can easily parse them to Java Date object
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = dateFormat.parse("2018-01-24T05:00:00.000Z");
for more details see https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
You can try something like this....
I did this in Eclipse....
public static void main(String[] args) throws Exception {
String jsonDate = "2018-01-24T05:00:00.000Z";
String[] split = jsonDate.split("-");
System.out.println(split[0]);
System.out.println(split[1]);
System.out.println(split[2].substring(0, 2));
String month = split[1].toString();
String day = split[2].substring(0, 2).toString();
String year = split[0].toString();
System.out.println(month + "-" + day + "-" + year);
}
output
2018
01
24
01-24-2018
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
private String formatDateFromString (String dateString) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS" );
Date date = simpleDateFormat.parse(dateString);
simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
return simpleDateFormat.format(date);
} catch (ParseException e) {
}
return "";
}
This question already has answers here:
Get Date type object in format in java
(6 answers)
Closed 6 years ago.
I want to convert String to Date using following format: "yyyy-MM-dd"
public class TestProgram {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -2);
Date toDelete = new Date(cal.getTimeInMillis());
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
String formatDate = s.format(toDelete);
System.out.println( "Date before 2 Days: " + formatDate);
}
}
Current datatype format is String, I want to convert this in Date datatype, how to do that with this format [yyyy-MM-dd].?
What you're looking for is:
//formatDate is String containing date
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = s.parse(formatDate);
You could do something like this just to get Date format:
String yourdate = "2015-04-08";
Date date = java.sql.Date.valueOf(yourdate);
Alternatively I recommend using Apache/Joda libraries for date manipulation.