Convert .net date to Java date - java

In my code, I'm receiving a date formatted: /Date(-82135555200000-0800)/
How can I parse it in Java to become a Java date?

In the string /Date(-82135555200000-0800)/
use this line of code to remove non-number element from the String
String tempDate = data.getDate().replaceAll("\\D+", "");
what you actually be interested in should be 8213555520000 . you would need first 15 digits to get the date from it as the given time is in milliseconds, you can do whatever you want with it.
Many languages follow this convention so dealing with date becomes easy
Date date = new Date(Long.parseLong(""+tempDate));
Or as following
String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(821355552*10000L));

Related

java.time.format.DateTimeParseException: Text '103545' could not be parsed at index 2

I'm trying to parse two different dates and calculate the difference between them, but the next error appears:
java.time.format.DateTimeParseException: Text '103545' could not be parsed at index 2
Here's the code:
String thisDate= mySession.getVariableField(myVariable).toString().trim();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy");
LocalDate theDate= LocalDate.parse(thisDate, formatter);
That’s as expected (approximately).
Your format pattern string, ddMMyyyy specifies two digits day of month, two digits month and (at least) four digits year for a total of (at least) eight (8) digits. So when you give it a string consisting of only 6 digits, parsing will necessarily fail.
If your user or another system is required to give you a date in ddMMyyyy format and they give you 103545, they are making an error. Your validation caught the error, which is a good thing. You will probably want to give them a chance to try again and give you a string like for example 10112021 (for 10 November 2021).
In case (just guessing) 103545 was meant to denote a time of day, 10:35:45 then you need to use the LocalTime class for it, and you also need to change the format pattern string to specify hours, minutes and seconds instead of year, month and date.
String thisDate = "103545";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmmss");
LocalTime theTime = LocalTime.parse(thisDate, formatter);
System.out.println(theTime);
Output from this snippet is:
10:35:45
The problem here is the date parser has to recieve a date in the format specified (in this case "ddMMyyyy")
For example, this is what you would need to input for the parser to return a valid date:
String thisDate = '25Sep2000';
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy");
LocalDate theDate = LocalDate.parse(thisDate, formatter);
I think what you want is to convert a date in milliseconds to a date with a specific format. This is what you can do:
//Has to be made long because has to fit higher numbers
long thisDate = 103545; //Has to be a valid date in milliseconds
DateFormat formatter = new SimpleDateFormat("ddMMyyyy"); //You can find more formatting documentation online
Date theDate = new Date(thisDate);
String finalDate = formatter.format(theDate);

SimpleDateFormat can not parse string to Date

I'm so ashamed asking this question, but I have no other choice.
I want to convert this string to date:
2015-11-25T19:36:39.571+06:00
To convert it I use SImpleDateFormat:
String str = "2015-11-25T19:36:39.571+06:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = format.parse(str);
System.out.println(date);
When I launch this code it gives to me java.text.ParseException: Unparseable date: exception.
I don't know why this is happening.
This should work (In java 7)
String str = "2015-11-25T19:36:39.571+06:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = format.parse(str);
System.out.println(date);
XXX is available in Java 7 as Timezone offset, see the Javadoc http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The issue is because of wrong pattern used.Z isInstead of yyyy-MM-dd'T'HH:mm:ss.SSSZ use pattern yyyy-MM-dd'T'HH:mm:ss.SSSX
Z represents Time zone in RFC 822 time zone format i.e., like this -0800
So, your code will look like this.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Your pattern does not correspond entirely to the input string, you have a slight type in the time zone part of your format.
Change your format in the following way to make it work:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Or change your input string to:
String str = "2001-07-04T12:08:56.235-07:00";
Please use Below Pattern it works fine
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
There is a problem with your input String according to the pattern you have used.
remove the last ':' from the str variable.
change:
String str = "2015-11-25T19:36:39.571+06:00";
in to:
String str = "2015-11-25T19:36:39.571+0600";
and run. It'll work.
Here is the fiddle:
working link

How to convert into Timestamp format in Java?

I am getting the date time in "1/23/2013 6:57:17 AM" format and need to convert it into "2012-01-01T12:00:00" format. I could have solved the issue by using string functions and separating the date and time and dealing with them individually. But the problem is compunded by the fact that the date format is M/D/YYYY and even time has only h:mm:ss which means i cannot assume the number of characters before each delimiter.
I hope someone has dealt with something like this before. Thanks.
No, String functions are not the way to go.
I'd recommend using two DateFormat instances: one for the source format and another for the target format.
DateFormat source = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
source.setLenient(false);
DateFormat target = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
target.setLenient(false);
String dateAsString = "1/23/2013 12:00:00 AM";
Date d = source.parse(dateAsString);
System.out.println(target.format(d));

convert string into date format in java

I want to convert this string to the following date format.
String s = "2-26-2013";
Date date = new SimpleDateFormat("EEEE, MMMM/dd/yyyy").parse(s);
System.out.println(date);
I'm getting this error:
Exception in thread "main" java.text.ParseException: Unparseable date: "2-26-2013"
at java.text.DateFormat.parse(DateFormat.java:357)
Well yes. The argument you pass into the constructor of SimpleDateFormat says the format you expect the date to be in.
"EEEE, MMMM/dd/yyyy" would be valid for input like "Tuesday, February/26/2013". It's not even slightly valid for "2-26-2013". You do understand that you're parsing the text at the moment, not formatting it?
It looks like you want a format string of "M-dd-yyyy" or possibly "M-d-yyyy".
If you're trying to convert from one format to another, you need to first specify the format to parse, and then specify the format to format with:
SimpleDateFormat parser = new SimpleDateFormat("M-dd-yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM/dd/yyyy");
Date date = parser.parse(input);
String output = formatter.format(date);
Date date = new SimpleDateFormat("MM-dd-yyyy").parse(s);
The argument to SimpleDateFormat defines the format your date it in. The above line matches your format, and works. Your example does not match.
Instead of using MMMM/dd/yyyy you need to used MM-dd-yyyy. SimpleDateFormat expects the pattern to match what its trying to parse.

Date conversion in Java

How can I take a string in a format such as: 2008-06-02 00:00:00.0 and convert it to: 02-Jun-2008?
Can I somehow take the original string, convert it to a Date object, then use a formatter to get the final output (rather than parsing the string myself)? Thanks!
You can use SimpleDateFormat to convert between a String and a Date object and vice versa based on a pattern. Click the API link, you'll see patterns being explained in detail. A 4-digit year can be represented with yyyy, a 3-character month abbreviation can be represented with MMM and so on.
First you need to parse the String of the first format into a Date object:
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf1.parse(inputString);
Then you need to format the Date into a String of the second format:
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String outputString = sdf2.format(date);
Note that you need to take the Locale into account as well to get the month to be printed in English, else it will use the platform's default locale to translate the month.
Use 2 instances of SimpleDateFormat class. One for converting your input string to date and second to convert date back to string but in another format.
Here is an example of using SimpleDateFormat.
DateFormat startFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat endFormat = new SimpleDateFormat("dd-MM-yyyy");
String outputString = null;
try {
Date date = startFormat.parse(inputString);
outputString = endFormat.format(date);
} catch(ParseException pe) {
throw new IllegalArgumentException(inputString + " is not properly formated.", pe);
}
You can definitely use SimpleDateFormat class like others have recommended.
Another suggestion if it applies in your case. If you are getting this data from a sql query you can also use to_char() method to format it in the query itself. For example: to_char(column_name,'DD-MON-YYYY')

Categories