I am getting unexpected results when I am running this code:
var DateFormat_in = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss");
var DateFormat_out = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
var m ="4/29/2016 12:12:12"
println(str_timestamp(m,DateFormat_in,DateFormat_out)) // third
def str_timestamp(date_str:String,format_in:SimpleDateFormat,format_out: SimpleDateFormat): DateTime =
{
val date = format_in.parse(date_str)
val date_modified_str = format_out.format(date)
println(date_modified_str) // first
val temp = format_out.parse(date_modified_str);
println(temp) // second
val dateTime: DateTime = new DateTime(temp.getTime)
dateTime
}
I am getting the following:
2016-12-29 12:12:12
Fri Jan 29 12:12:12 CET 2016
2016-01-29T12:12:12.000+01:00
why the month is changing from one to one ? How to get it right ?
Please consult the javadoc of SimpleDateFormat. The correct pattern symbol for months is "M" (capital letter) and for minutes "m" (small).
Related
How i can extract the Time alone in this format "2022-07-26T12:10:07.000+0000" and show in 12hrs.
The output i am supposed to get is "5:40 PM", but i am getting 12.10 AM.
How to get exact time in the above mentioned format?
This is the method i followed to parse the DateTime.
String getTimeStringNew(String date) {
DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
var dateValue = dateFormat.parse(date);
DateFormat dateFormat1 = DateFormat("hh:mm a");
var value = dateFormat1.format(dateValue);
return value;}
In this method i am getting "12:10 AM".
But correct time is "5.40 PM".
What is the mistake i am doing here. Please correct.
2022-07-26T12:10:07.000+0000
Here 2022 is year
07 is month
26 is day
12 is hour
10 is minutes
07 is seconds.
So you will get 12.10 only
Maybe you are recieving the time in UTC.
You can use .toLocal to convert it to local time
var dateLocal = dateUtc.toLocal();
Edit
String getTimeStringNew(String date) {
DateTime _localDate = DateTime.parse(date).toLocal();
DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
var dateValue = dateFormat.format(_localDate);
DateFormat dateFormat1 = DateFormat("hh:mm a");
var value = dateFormat1.format(_localDate);
return value;}
I'm trying to format a date to the following format, UTC with the Timezone Offset:
Expected:
2021-01-20T21:00:00-06:00
Actual:
2021-01-20T00:00:00-06:00
Code:
var arrivedAt = "2021-01-20T15:00:00"
var timezone = "America/Chicago"
var finalDateTime = (((arrivedAt as Date) as String {format: "yyyy-MM-dd'T'00:00:00"}) as LocalDateTime ++ (timezone as String))
I'm assuming it's due to the "00:00:00" in the format but when I try using "HH" or "hh" I'm receiivng errors.
The good news is that it's documented here https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-change-time-zone
This example code assigns a timezone to the original date and then shift it to UTC
var arrivedAt = "2021-01-20T15:00:00"
var timezone = "America/Chicago"
var dateWithTimezone = (arrivedAt as LocalDateTime) ++ "America/Chicago"
// dateWithTimezone = "2021-01-20T15:00:00-06:00"
var dateOnUTC = dateWithTimezone >> "UTC"
// dateOnUTC = "2021-01-20T21:00:00Z"
---
dateOnUTC as String { format: "yyyy-MM-dd'T'HH:mm:ssXXX"}
NOTE: I'm not sure if the expected value is right on the question or if the example date is wrong.
It looks like you are using the incorrect data types and trying to do the date transformations as strings. That is highly unadvised. The errors are probably because a Date doesn't has a time neither a timezone.
One alternative is to treat the date time as a DateTime, to be able to use the timezone features:
%dw 2.0
output application/json
var arrivedAt = "2021-01-20T15:00:00"
var timezone = "America/Chicago"
var finalDateTime = (arrivedAt as DateTime >> timezone ) as String {format: "yyyy-MM-dd'T'HH:mm:ssZ"}
---
finalDateTime
Output
"2021-01-20T09:00:00-0600"
Actually the correct hour seems 9 instead of 21 as in your expected result. Maybe you did the timezone transformation the other way around?
Initially, I had used string format for the field timestamp and I was able to convert it to 12 December 2017 2:34:23 format using toData().toString(). I need to do the same thing when the field timestamp is in timestamp format. Can I get some help on this?
You can get a DateTime variable from a timestamp using
var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp);
Once you have your date you can format however you want using a DateFormat for example:
var formatter = new DateFormat('yyyy-MM-dd'); // You can change the format here
String formatted = formatter.format(date);
print(formatted); // Something like 2019-04-20
just check out this example below :
var timestamp =1583772663;
var format = new DateFormat('dd MMMM yyyy, hh:mm:ss');
var date = new DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
var value = format.format(date);
print(value);
//output : 09 March 2020, 10:21:03
let me know if it works.
In the following code, i want to find the date of the last Monday.
For that, i have two variable :
startDay = today - 7 days
stopDay = today - 1 day (yesterday)
And i have a function that list all dates between "startDay" and "stopDay", and search in these dates, which one corresponds to Monday.
It works well when i have two dates in the same ten :
startDay = 2014-07-20
stopDay = 2014-07-29
But, when one of both change decade, the code end with an error:
startDay = 2014-07-29
stopDay = 2014-07-30
ERROR:
java.lang.IllegalArgumentException: Incompatible Strings for Range: String#next() will not reach the expected value
CODE:
def searchDay = { start, stop -> (start..stop).findAll { Date.parse("yyyy-MM-dd", "${it}").format("u") == "1" } }
def startDay = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date()-7)
def stopDay = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date()-1)
def dateOfTheDay = searchDay(startDay, stopDay);
def dateOfTheDayWithoutSquare = dateOfTheDay.join(", ")
return dateOfTheDayWithoutSquare
This will find the previous Monday starting from today
def cal = Calendar.instance
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DAY_OF_WEEK, -1)
}
Date lastMonday = cal.time
// print the date in yyyy-MM-dd format
println lastMonday.format("yyyy-MM-dd")
If you want to find the Monday previous to some other date replace the first line with:
def cal = Calendar.instance
Date someOtherDate = // get a date from somewhere
cal.time = someOtherDate
This should be a touch faster (no loop):
def cal = Calendar.instance
def diff = Calendar.MONDAY - cal.get(Calendar.DAY_OF_WEEK)
cal.add(Calendar.DAY_OF_WEEK, diff)
cal.time.format("yyyy-MM-dd")
Question 1: I have 2 fields to let user enter start date and end date, but in string format
- DateStart (string: yyyy/mm/dd hh:mm)
- DateEnd (string: yyyy/mm/dd hh:mm)
May I how to compare both datetime? I want to know total how many hours is difference between the both date.
Question 2: user will enter 1 returnDate (string: yyyy/mm/dd hh:mm) also in string format, may I know how to update the returnDate if I will need to add 55hours on the returnDate?
Thanks
Start by taking a look at SimpleDateFormat, which will allow you to convert the String value to a Date object.
For example...
try {
// Note hh is Hour in am/pm (1-12), based on you example, it's not possible
// now the day part (ie am or pm), you could supply aa as the am/pm marker
// or use HH which is Hour in day (0-23)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm");
Date date = sdf.parse("2014/04/11 4:46");
System.out.println(date);
} catch (ParseException exp) {
exp.printStackTrace();
}
Then you can use JodaTime to calculate the difference between the two dates, see How to find difference between two Joda-Time DateTimes in minutes for an example
It should be noted that you could skip the use of SimpleDateFormat and JodaTime all the way, check out String to joda LocalDate in format of "dd-MMM-yy" for an example of converting a String to a LocalDate using JodaTime
To add time to an existing Date, you can use either Calendar or JodaTime, see how to add days to java simple date format for an example of both
I would recommend that if you are using some kind of GUI, you might consider using one of the available date pickers as it will save you a lot of hassel
I suggest using Java8's java.util.time package
Example:
public static void main(String[] args) {
// example input
String dateString1 = "2014/04/10 00:00";
String dateString2 = "2014/04/11 23:59";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
LocalDateTime date1 = LocalDateTime.parse(dateString1, dtf);
LocalDateTime date2 = LocalDateTime.parse(dateString2, dtf);
// do your stuff with the dates...
}
Here is my solution:
String date1 = "2014/04/10 15:30";
String date2 = "2014/04/11 09:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
try {
Date parsedDate1 = sdf.parse(date1);
Date parsedDate2 = sdf.parse(date2);
double secs = (parsedDate2.getTime() - parsedDate1.getTime()) / 1000;
double hours = secs / 3600;
System.out.println(hours);
} catch (ParseException e) {
e.printStackTrace();
}
For adding hours to a date:
Date date = new Date(someDateObject.getTime() + 55 * 3600 * 1000);