How to validate dates in two different formats [duplicate] - java

This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Closed 5 years ago.
I am doing some junit test. I am getting the date as response in the form of date as below :
2017-08-14 00:00:00.0 +0:00
The data which is present in oracle DB is
14-AUG-17 12.00.00.000000000 AM +00:00
I want to use an assert like this but it is failing. can anyone help to make sure that both expected and actual matches.
Assert.assertEquals("14-08-2017", 2017-08-14 00:00:00.0 +0:00);

You can create two Date objects and for the assertion.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
try {
Date parsedDate1 = formatter.parse("14-08-2017");
System.out.println(parsedDate1);
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate2 = formatter2.parse("2017-08-1 00:00:00.0 +0:00");
Assert.assertEquals(parsedDate1, parsedDate2);
} catch (ParseException e1) {
}

You can use SimpleDateFormat to make string from date.
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Assert.assertEquals("14-08-2017", sdf.format(<your date instance>));

Related

How to decode the date in Android? - DATETIMESTAMP [duplicate]

This question already has answers here:
Android: Compare time in this format `yyyy-mm-dd hh:mm:ss` to the current moment
(5 answers)
Conversion of a date to epoch Java [duplicate]
(4 answers)
How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?
(16 answers)
Closed 2 years ago.
The following code gave me Datetimestamp as [ 2020-07-183 17:07:55.551 ]. The issue is with "Day" in Datetimestamp, which has three digits. How to format currentTimeMillis into the right format for day of month?
public String Datetimesetter(long currentTimeMillis, SimpleDateFormat dateFormat) {
dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentTimeMillis);
return dateFormat.format(calendar.getTime());
}
SOLUTION WHICH WORKED FOR ME:
Please visit this link.
This is for the case you are supporting Apps from API level 26 (native support of java.time) or you are willing / allowed to use a backport library of the same functionality.
Then you can use a correct / matching pattern (one that considers three-digit days) like this:
public static void main(String[] args) {
// mock / receive the datetime string
String timestamp = "2020-07-183 17:07:55.551";
// create a formatter using a suitable pattern (NOTE the 3 Ds)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-DDD HH:mm:ss.SSS");
// parse the String to a LocalDateTime using the formatter defined before
LocalDateTime ldt = LocalDateTime.parse(timestamp, dtf);
// and print its default String representation
System.out.println(ldt);
}
which outputs
2020-07-01T17:07:55.551
So I guess the day of year no. 183 was actually July 1st.
your date format is incorrect
dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
change to this
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS.SSS");

Java SimpleDateFormat: Pattern - ParseException [duplicate]

This question already has answers here:
Datetime parsing error
(2 answers)
Closed 5 years ago.
I am struggling with a date string, I need to parse into the java ‘Date’ object.
Here is what I have got so far:
try {
String value = "2017‎-‎11‎-‎23T14:00:49.184000000Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'";
SimpleDateFormat parser = new SimpleDateFormat(pattern);
Date date = parser.parse(value);
} catch (ParseException e) {e.printStackTrace();}
It currently throws a ParseException “Unparseable date” and I can’t get it to work.
Any help is highly appreciated!
Thanks
Use Instant from java.time package (java 8) instead, it should look like below
String value = "2017-11-23T14:00:49.184000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
System.out.println(date);
you can use timeZone as well like this as another solution.
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2017-11-23T14:58:00.184000000Z"));
Date date = cal.getTime();
System.out.println(date);

Set timezone to IST, but I still get a wrong-date error [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Closed 5 years ago.
I want to take current time irrespective to the system date. I am using Glassfish server and derby in netbeans. I tried the code below for getting current date according to IST:
DateFormat df = new SimpleDateFormat("yyyymmdd");
df.setTimeZone(TimeZone.getTimeZone("ist"));
String gmtTime = df.format(new java.util.Date().getTime());
java.util.Date parsed = null;
try {
parsed = (java.util.Date) df.parse(gmtTime);
} catch (ParseException ex) {
Logger.getLogger(EmployeePanel.class.getName()).log(Level.SEVERE, null, ex);
}
java.sql.Date date = new java.sql.Date(parsed.getTime());
but I am getting 2017-01-08 instead of 2017-08-08.
m is minute not month (M). The right pattern is:
SimpleDateFormat("yyyyMMdd");
The pattern definition is case sensitive!
For more informations see the javadoc of SimpleDateFormat

Java Convert given date to date format like MM/dd/yyyy [duplicate]

This question already has answers here:
converting date format
(5 answers)
Closed 6 years ago.
I have a situation where i can get only the date like 9/2/2016. From this, i need to parse and create a pattern like M/D/YYYY. From this i can convert the any input date to the above pattern M/D/YYYY.
Is there any utility/API for that in java.?
For your requirement, first you parse the input string with appropriate format. Then you do format the date. For more info refer SimpleDateFormat
SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
Date myDate = dateFormat.parse("9/2/2016");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(dateFormat2.format(myDate));
Output
09/02/2016
You can use SimpleDateFormat to do this.
SimpleDateFormat dateFormat = new SimpleDateFormat(MM/dd/yyyy);
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

JSON date to formatted JAVA date [duplicate]

This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I am trying to get dd.MM.yyyy and hh:mm from 1436536800 but only the time is correct, the date is completely wrong. I don't really understand how this is supposed to work
int dt = time.getInt("start")*1000;
Date date = new Date(dt);
startDate = dateFormat.format(date);
If time.getInt("start") is a valid unix timestamp, you must add "000" to the number. Example: 1436536800 * 1000 = 1436536800000. Then you can use the timestamp to get a Date:
final Date date = new Date(Long.parseLong("1436536800000"));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(sdf.format(date));
Console exit: 10.07.2015 09:00
Assuming the time is correct, it's likely the fact that you're multiplying by 1,000. When creating the date the way you are, it takes in milliseconds. Is it possible that your input is already in milliseconds? (Your current method will be ~2 minutes off if so)
Date date=new Date(1436536800);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
Date you are getting is a JSON string value. follow steps below to format it correctly.
First download Moment.js file and add it in your project.
var date1 = "1436536800"; // your long value contain in this variable.
var date2 = moment(date1).format(MMMM Do YYYY);//It will give you formatted date value.
see more formats below

Categories

Resources