I want to get the current date so I used:
Calendar.getInstance().getTime()
But as it .getTime() it is returning:
Fri Jul 11 15:07:03 IST 2014
I want only date in any format but without the time.
scala> java.time.LocalDate.now
res4: java.time.LocalDate = 2014-07-11
You may use formatting:
val format = new SimpleDateFormat("d-M-y")
println(format.format(Calendar.getInstance().getTime()))
Since Java 8, you can also use LocalDate class:
println(java.time.LocalDate.now)
OR
java.time.LocalDate.now.toString
val dateFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mm aa")
var submittedDateConvert = new Date()
submittedAt = dateFormatter.format(submittedDateConvert)
You need to use SimpleDate formate method. You can specify which formate you want.
SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yy");
String dateSelected = formatter.format(new Date());
In case if you want to format the date in your way.
println(DateTimeFormatter.ofPattern("dd-MM-YYYY").format(java.time.LocalDate.now))
if someone needs the date in a format so that to be able to add to PostgreSQL here we go:
import java.util.Calendar
val calendar = Calendar.getInstance
val now = new Timestamp(calendar.getTime.getTime)
or in one line:
val now = new Timestamp(java.util.Calendar.getInstance.getTime.getTime)
Related
MongoCollection<Document> Profile_List = db.getCollection("Profile_List");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
Date todaydate = format.parse(new Date().toString());
ArrayList<Document> activeList=profile.find(Filters.regex("lastActive",todayDate.toString())).into(new ArrayList<Document>());
This is the code what we have written. We are getting an “Unparseable date error”. Can someone please help?
This is wrong:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
Date todaydate = format.parse(new Date().toString());
The expression new Date().toString() does not return a string that conforms to the format yyyy-MM-DD, so if you try to parse it as if it is formatted that way, you will get an exception.
If you want a Date object that represents the current date and time, simply do this:
Date todaydate = new Date();
No need to convert the Date object to a string and trying to parse it.
If you need a string with the current date in the format yyyy-MM-dd then do this:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String todaydate = format.format(new Date());
Note: You used DD in your date format string but you most likely meant dd. See the API documentation of SimpleDateFormat.
If you are trying to get the current date string in yyyy-MM-dd format. You can do format it like this
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = simpleDateFormat.format(new Date());
I currently have some TestNG custom reporting code that works with the local MySQL database I have been testing against. The resulting database is postgres due to issues with latest MySQL versions in AWS, in trying to convert the format of TestNG millis over I have been encountering issues with the format which I can't seem to get one that works right.
My custom report code was using the following:
report.reporting.put("startDate", testResult.getStartMillis());
report.reporting.put("endDate", testResult.getEndMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
String startDbTime = sdf.format(report.get("startDate"));
String endDbTime = sdf.format(report.get("endDate"));
When I try some of the dateformatters I am receiving indexing errors like: DateTimeParseException: Text could not be parsed at index 4
I've used some various options like offset or instant with no success.
This is a collection of some of the options I have tried.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
String startDbTime = sdf.format(report.get("startDate"));
String endDbTime = sdf.format(report.get("endDate"));
// OffsetDateTime startDbTime = OffsetDateTime.parse(startRawDbTime);
// OffsetDateTime endDbTime = OffsetDateTime.parse(endRawDbTime);
// ZonedDateTime startDbTime = ZonedDateTime.parse(startRawDbTime);
// ZonedDateTime endDbTime = ZonedDateTime.parse(endRawDbTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
String startRawDbTime = sdf.format(report.get("startDate"));
String endRawDbTime = sdf.format(report.get("endDate"));
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDate startParse = LocalDate.parse(startRawDbTime,formatter);
LocalDate endParse = LocalDate.parse(endRawDbTime,formatter);
Long startTimeRaw = Long.parseLong(report.get("startDate").toString());
Instant startInst = Instant.ofEpochMilli(startTimeRaw);
ZonedDateTime zoneStart = ZonedDateTime.ofInstant(startInst, ZoneOffset.UTC);
LocalDate dateStart = formatter.format(zoneStart);
Is there a conversion step I am missing? I thought it would be simple to convert from millis to something that postgres would accept.
Worked out how to do this with PostGres by resetting the DB table to be a date with time zone, then adjusted my Java code to the following:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startRawDbTime = sdf.format(report.get("startDate"));
String endRawDbTime = sdf.format(report.get("endDate"));
Timestamp timeStart = Timestamp.valueOf(startRawDbTime);
Timestamp timeEnd = Timestamp.valueOf(endRawDbTime);
With the statement insert updated with the right type:
reportInsert.setObject(4,timeStart, Types.TIMESTAMP);
reportInsert.setObject(5,timeEnd, Types.TIMESTAMP);
Everything works as expected now.
I am trying to add time to groovy parameter which have DateTime stored in SimpleDateFormat.
import groovy.time.TimeCategory
import java.text.SimpleDateFormat
def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
log.info startdatetime
aaa = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(startdatetime)
use(TimeCategory)
{
def enddatetime = aaa + 5.minutes
log.info enddatetime
}
startdatetime : Wed Nov 08 19:57:50 IST 2017:INFO:2017-11-08T15:00:00.000Z
Error popup displayed with message
'Unparseable date: "2017-11-08T15:00:00.000Z"'
If the date string is Wed Nov 08 19:57:50 IST 2017 and you want to convert it to date object, then you could do:
def dateString = "Wed Nov 08 19:57:50 IST 2017"
def dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
def date = Date.parse(dateFormat, dateString)
Looks you wanted to add 5 minutes to it which can be done as did already
def endDate
use(TimeCategory) { endDate = date + 5.minutes }
log.info "End date : $endDate"
If you want the date object to formatted, then do:
def outputDateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
log.info "Formatted date: ${date.format(outputDateFormat)}"
Another suggestion after looking at your code to get the project property value, use below one-liner.
Change From:
def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
To:
def startDateTime = context.expand('${#Project#StartDateTime}')
Instead of "yyyy-MM-dd'T'HH:mm:ss'Z'" you probably want "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" since your input string includes milliseconds.
I have got no experience with Groovy, but I assume that since you can use Java classes, you can also use the modern Java date and time API. I much recommend that over the long outdated SimpleDateFormat class. Your format, 2017-11-08T15:00:00.000Z, is in no way tied to SimpleDateFormat, on the contrary, it’s ISO 8601, the format that the modern date and time classes (as opposed to the old ones) “understand” natively without the need for an explicit formatter for parsing.
So I suggest you try (by no means tested):
import java.time.Instant
import java.time.temporal,ChronoUnit
and
aaa = Instant.parse(startdatetime)
and maybe (if you still need or want to use the Java classes)
enddatetime = aaa.plus(5, ChronoUnit.MINUTES)
I have to parse dates in formats:
HH:mm
dd MMM
dd MMM yyyy
I've managed to handle the last two of them:
val dateParsers = Array(
DateTimeFormat.forPattern("dd MMM").getParser,
DateTimeFormat.forPattern("dd MMM yyyy").getParser,
ISODateTimeFormat.timeParser().getParser
)
val formatter = new DateTimeFormatterBuilder().append(null, dateParsers).toFormatter.withZoneUTC
DateTime.parse(updatedString, formatter.withDefaultYear(currentYear).withLocale(ruLocale))
Everything is ok with dd MMM and dd MMM yyyy, but when I'm trying parse time like 05:40 I'm getting 01-01-1970 date instead of today's date. What is the simplest method to set default date as today's date in parser?
Joda-Time-Formatter only supports withDefaultYear(), not things like withDefaultDate(). Instead you can do this:
DateTimeFormatter timeParser = DateTimeFormat.forPattern("HH:mm");
LocalTime time = timeParser.parseLocalTime("05:40");
DateTimeZone tz = DateTimeZone.getDefault(); // Or DateTimeZone.UTC or DateTimeZone.forID( "Europe/Paris" )
LocalDate today = LocalDate.now(tz);
DateTime moment = today.toLocalDateTime(time).toDateTime(tz);
System.out.println(moment);
// output in my local timezone: 2014-08-20T05:40:00.000+02:00
Note: I have written the solution in Java, because I am not a scala-guy.
Date and Time are completely different, without subclassing DateTimeFormatter and implementing your special "time at todays' date"-algorithm you wont get very far. Either subclass or maybe inject your current date into the string if it matches some regular expression
I'd use withDate()
private static final DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("HH:mm");
#Test
public void test() {
DateTime dateTime = FORMATTER.parseDateTime("05:40");
DateTime now = new DateTime();
dateTime = dateTime.withDate(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth());
System.out.println(dateTime.toDate());
}
I have a datetime format in XML and I'm trying to unmarshall the values as follows:
2013-03-17T19:12:14Z -> 2013-03-17 19:12 +0100
I have used Joda's DateTime and a DateTimeAdapter class to override the unmarshalling. The datetime format is coming out weird, as follows:
{"iMillis":1363510800000,"iChronology":{"iBase":{"iBase":{"iBase":
{"iMinDaysInFirstWeek":4}},"iParam":{"iZone":{"iTransitions":
[-9223372036854775808,-3852662325000,-1691964000000,-1680472800000,
-1664143200000,-1650146 400000,-1633903200000,-1617487200000,
-1601848800000,- etc etc.
Can anyone help me format this date?
I was unable to figure out the answer so I've tried the following:
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm Z");
DateTime dateTime = new DateTime(v);
long dateTimeMiliSec = dateTime.getMillis();
Date date = new Date(dateTimeMiliSec);
return sd.format(date);
So 2013-03-17T09:00:00Z converts to 2013-03-17 09:00 +0000