convert epoch json format string to date on java [duplicate] - java

This question already has answers here:
How to convert Timestamp into Date format in java
(4 answers)
Closed 2 years ago.
I have a string I recieve from service is a date, this date have this format "/Date(1607490140063-0600)/" is a string, now I need convert from a string to a date format on java, I try differents ways but no work
String endTime = getPunchTime();
SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z");
Date d2 = timeFormat.parse(endTime);

Write a helper method for parsing the value, and use regex to parse the text, e.g. like this:
static OffsetDateTime parseJsonDate(String s) {
Pattern p = Pattern.compile("/Date\\((-?\\d+)([+-]\\d{4})\\)/");
Matcher m = p.matcher(s);
if (! m.matches())
throw new DateTimeParseException("Not a valid JSON Date string: " + s, s, 0);
return Instant.ofEpochMilli(Long.parseLong(m.group(1))).atOffset(ZoneOffset.of(m.group(2)));
}
Test
System.out.println(parseJsonDate("/Date(1607490140063-0600)/"));
Output
2020-12-08T23:02:20.063-06:00

Related

Format a date in java [duplicate]

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 5 years ago.
I have a date in String format: 2017-05-10.
I need to transform this string in this 10/05/2017. How i can do?
Which library i can use?
I have tried this but not works
DateFormat df = new SimpleDateFormat("dd-MM-yyyy",Locale.ITALIAN);
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(data));
Can also be done using the java.time API:
String date = "2017-05-10";
LocalDate ld = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String formattedDate = DateTimeFormatter.ofPattern("dd/MM/yyyy").format(ld);
You can try following code:
DateFormat dfto = new SimpleDateFormat("dd/MM/yyyy");
DateFormat dffrom = new SimpleDateFormat("yyyy-MM-dd");
Date today = dffrom.parse("2017-05-10");
String s = dfto.format(today);
I would simply run an internal Java function that comes built in with the String object, described below:
replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of
oldChar in this string with newChar.
String myDate = df.parseDate(data).toString();
// if that's the right string you mention
// then
cal.setTime(myDate.replace('-', '/'));
They may have to be escaped but since it's char data type my initial thought is no.

Convert a Date dd/MM/yyyy to dd-MM-yyyy in java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I'm trying to convert a date in dd-MM-yyyy format from YYYY-MM-dd hh:mm:ss.ms
in java using the below code but m unable to get the desired value
String dob = (new SimpleDateFormat("dd-MM-yyyy")).format(customerEntity.getDob().toString());
customerEntity.getDob().toString is providing me this value 1987-06-12 00:00:00.0
But when i'm parsing it to the String dob it produces 163-06-1987 as the output whereas i want the output like 12-06-1987 .
Any help will be appreciable, thanks well in advance
format method in SimpleDateFormat take a Date as argument and not a String
public static void main(String[] args) {
String dateStr = "29/12/2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
Try parsing your string date into a Date first in format it is coming. Post that pass on that Date object to a format in the format you want your output.
As in below :
Date dob = (new SimpleDateFormat("yyyy-MM-dd")).parse("1987-06-12 00:00:00.0");
String dob1 = (new SimpleDateFormat("dd-MM-yyyy")).format(dob);

Java - Converting yyyy-MM-dd'T'HH:mm:ssZ to readable dd-MM-yyyy [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
I will have a String input in the style of:
yyyy-MM-dd'T'HH:mm:ssZ
Is it possible to convert this String into a date, and after, parsing it into a readable dd-MM-yyyy Date Object?
Yes. It can be done in two parts as follows:
Parse your String to Date object
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date dt = sd1.parse(myString);
Format the Date object to desirable format
SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
String newDate = sd2.format(dt);
System.out.println(newDate);
You will have to use two different SimpleDateFormat since the two date formats are different.
Input:
2015-01-12T10:02:00+0530
Output:
2015-01-12
//Parse the string into a date variable
Date parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);
//Now reformat it using desired display pattern:
String displayDate = new SimpleDateFormat("dd-MM-yyyy").format(parsedDate);

Converting date: d-M-yyyy to dd-MM-yyyy [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Conversion of Date
I get the date from dialog as: 5-1-2012 - as a String.
I need to convert it to 05-01-2012, string as well. What is the simpliest way to do it?
Do you mean?
String date = "5-1-2012";
if (date.charAt(1) == '-') date = "0" + date;
if (date.charAt(4) == '-') date = date.substring(0,3) + "0" + date.substring(3);
// date is 05-01-2012
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");
String format = s.format(new Date());
If you plan to use it as a Date object later, I would use the SimpleDateFormat parse() function...
you may test your string to determine whether to use a d-M-yyyy or dd-MM-yyyy pattern for the parser

How to convert Date represented as a String to milliseconds? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert java string to Date object
In my Java code I have input String like 05.10.2011 which I need to convert to milliseconds.
So:
String someDate = "05.10.2011";
I have to convert to match milliseconds format.
String someDate = "05.10.2011";
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
Date date = sdf.parse(someDate);
System.out.println(date.getTime());
You can use Java's SimpleDateFormat to easily convert to a Date instance.
SimpleDateFormat formatter = new SimpleDateFormat("MM.dd.yyyy"); // Month.Day.Year
Date d = formatter.parse(inputString);
long timestamp = d.getTime();
use the simpleDateFormat which takes a string and converts it to Date then call the getTime() to get the date in milliseconds format

Categories

Resources