Parse date from JSON to Java [duplicate] - java

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

Related

Date formatter numbers to text [duplicate]

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.

DateString format Conversion in Java [duplicate]

This question already has answers here:
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 5 years ago.
Can anyone tell me how to convert this dateString "2003Jan01" into another dateString which is in this format "01-JAN-03"?
One way is using two SimpleDateFormat: one to parse the input, one to format the output:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SDF
{
public static void main(String args[])
{
SimpleDateFormat source = new SimpleDateFormat("yyyyMMMdd", Locale.ENGLISH);
try
{
Date date = source.parse("2003Jan01");
SimpleDateFormat destination = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
System.out.println(destination.format(date));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Code for matching date Pattern [duplicate]

This question already has answers here:
Java: Check the date format of current string is according to required format or not [duplicate]
(8 answers)
Closed 6 years ago.
I have a scenario where I get the date value from the xml and the date value from the website. I am able to retrieve the results, But now I need to compare the format of the retrieved results whether it is in 'MM YYYY' format or not ? How can i do that ? i have to check the format in String manner ie for e.g Apr 2010 but not 04 2010
Please can anyone of you help me ??
public class SimpleDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
String strDate = sdf.format(date);
sdf = new SimpleDateFormat("MMM/yyyy");
strDate = sdf.format(date);
System.out.println(strDate);
}
}
You can use a regular expresson, like this (Jan|Feb|Mar...) d{4}

Convert string to date with format in java [duplicate]

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.

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