How to check the displayed date in Selenium with Java - java

I am trying to get the displayed date in the exact format to validate if it is in MM/dd/yyyy HH:mm. I tried :
element.getAttribute("value")
but that returns "yyyy-MM-ddTHH:mm" which actually is different than what is in UI.
Also when I use:
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMyyyy");
String date = dtf.format(currentDateTime);
works fine: screenshot

The data from the UI may have been converted into ISO-8601 format by the bootstrap library. Therefore, if you need to get the value in the MM/dd/yyyy HH:mm format, you can do so using a DateTimeFormatter.
Demo:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String input = "2021-06-01T04:05";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/uuuu HH:mm", Locale.ENGLISH);
String formatted = LocalDateTime.parse(input).format(formatter);
System.out.println(formatted);
}
}
Output:
06/01/2021 04:05
ONLINE DEMO
Learn more about the modern Date-Time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Related

Java JodaTime: java.lang.NoClassDefFoundError: org/joda/time/Chronology

I am writing a basic Java program using JodaTime to convert a date from the Gregorian Calendar to the Islamic Hijri Calendar. However, when I run my code, I get the following error:
Error: Unable to initialize main class MainActivity Caused by:
java.lang.NoClassDefFoundError: org/joda/time/Chronology
Below is my code:
import org.joda.time.Chronology;
import org.joda.time.LocalDate;
import org.joda.time.chrono.IslamicChronology;
import org.joda.time.chrono.ISOChronology;
public class MainActivity {
public static void main(String[] args) {
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
LocalDate todayIso = new LocalDate(2021, 8, 17, iso);
LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(),
hijri);
System.out.println(todayHijri);
}
}
This seems strange, considering that I have downloaded the latest joda time jar file from the official Joda Time release history on GitHub: https://github.com/JodaOrg/joda-time/releases (joda-time-2.10.10.jar), added it to the lib folder in my project, and added the jar file to my build path, as you can see in the file hierarchy below:
As #Abra stated, making sure the JAR file was on my run configuration in my classpath worked :D.

Time refresh in the console

I tried to program date and time so that it corresponds to the current date but could not find a solution in the console, only the date of the last run is displayed. Here is my code:
public class DateDemo {
public static void main(String args[]) {
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E dd.MM.yyyy 'um' HH:mm:ss ");
System.out.println("Datum: " + ft.format(dNow));
}
}
Consoles behave differently, so I’m afraid that there isn’t any universal solution.
I tried this:
DateTimeFormatter formatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
.withLocale(Locale.GERMAN);
ZoneId timeZoneId = ZoneId.of("Europe/Zurich");
int totalIterations = Math.toIntExact(TimeUnit.HOURS.toSeconds(2));
for (int sec = 0; sec < totalIterations; sec++) {
System.out.print(ZonedDateTime.now(timeZoneId).format(formatter) + '\r');
TimeUnit.SECONDS.sleep(1);
}
When I run it inside Eclipse, it prints the lines under each other in the Eclipse console like this:
16. November 2019 07:37:32 MEZ
16. November 2019 07:37:33 MEZ
16. November 2019 07:37:34 MEZ
16. November 2019 07:37:35 MEZ
But when I run it from the Terminal window on my Mac, it stays on the same line and just keeps overwriting it:
The \rcharacter is a carriage return. In theory it should go back to the beginning of the same line. Maybe the reason why it doesn’t in Eclipse is they thought I wanted to use the console window for debugging and therefore would be better served if I could see all output, so they didn’t want to overwrite any. Just guessing.
There are more elegant and more accurate ways to make something happen every second. Look into ScheduledExecutorService.scheduleAtFixedRate.
PS The format differs a bit in the two screen shots, in the Terminal window um (“at”) is included between the date and the time. The difference comes from different Java versions. In Eclipse I used Java 8, in the Terminal window Java 11. The story is in this question: JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9.

How to Format English Date in Japanese with ERA

I want the new Japanese ERA Date as "R010501", whereas I am getting "R151".
I am using the com.ibm.icu.text.DateFormat package to get the date format
Date dtEngDate = new SimpleDateFormat("yyyy-MM-dd").parse("2019-05-01");
com.ibm.icu.util.Calendar japaneseCalendar = new com.ibm.icu.util.JapaneseCalendar();
com.ibm.icu.text.DateFormat japaneseDateFormat = japaneseCalendar.getDateTimeFormat(
com.ibm.icu.text.DateFormat.SHORT, -1, Locale.JAPAN);
String today = japaneseDateFormat.format(dtEngDate);
System.out.println("today is:" +today.replaceAll("/", ""));
Output: today is --> R151.
Expected Output: today is --> R010501
I don't know what exactly you did other than me but I just downloaded the com.ibm.icu library from http://www.java2s.com/Code/Jar/c/Downloadcomibmicu442jar.htm and basically copied your code.
import com.ibm.icu.text.DateFormat;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.JapaneseCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
Date dtEngDate = new SimpleDateFormat("yyyy-MM-dd").parse("2019-05-01");
Calendar japaneseCalendar = new JapaneseCalendar();
DateFormat japaneseDateFormat = japaneseCalendar.getDateTimeFormat(DateFormat.SHORT, -1, Locale.JAPAN);
String today = japaneseDateFormat.format(dtEngDate);
System.out.println("today is: " + today.replaceAll("/", ""));
}
}
I'm getting today is: 平成310501 as console output and I guess this is what you are looking for. So I guess there is something wrong with your com.ibm.icu-4.4.2.jar.
Maybe consider retrying to download the latest version from the link I used and adding it to the modules/projects dependencies.
java.time.chrono.JapaneseDate
You don’t need an external dependency for the Japanese calendar. It’s built-in, in the JapaneseDate class.
Beware: Only the most recent versions of Java know about the new Reiwa era.
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("GGGGGyyMMdd", Locale.JAPANESE);
LocalDate isoDate = LocalDate.parse("2019-05-01");
JapaneseDate japaneseDate = JapaneseDate.from(isoDate);
System.out.println(japaneseDate.format(dateFormatter));
Output from this snippet on Java 11.0.3 is:
R010501
Five pattern letters G gives the narrow form of the era, just one letter (here R for Reiwa).
On Java 9.0.4 I got H310501. So it seems that it will work correctly if you are able to upgrade your Java. Meno Hochschild reports in a comment that he got the correct result on Java 8u212, so you may not need to upgrade to a new major version if only you’ve got the newest minor upgrade within your major version. I don’t know if there’s a way to upgrade only the calendar data in an older Java version, it might be another thing to investigate.
BTW don’t use SimpleDateFormat and Date. Those classes are poorly designed (the former in particular notoriously troublesome) and long outdated. Use java.time, the modern Java date and time API. It’s so much nicer to work with.
Link: Oracle Tutorial: Date Time explaining the use if java.time.

How to form java.util.Date calls in Clojure [duplicate]

This question already has answers here:
How to Convert A Clojure/Java Date to Simpler Form
(2 answers)
Closed 8 years ago.
I have been successfully using java.util.Date, and I would prefer to keep using it. Basically, I am having trouble forming the method calls to a class.
I basically want to feed a date like 2014-08-06 into my Clojure program which will override using today as the date. This is so I can form a SQL query. I'm just not sure how to use the Java calls in Clojure.
(def x1 (SimpleDateFormat. "yyyy-MM-dd"))
I just don't know how to form the parse.
Here's my core.clj
(ns util.core
^{:author "Charles M. Norton",
:doc "util is a Clojure utilities directory containing things
most Clojure programs need, like cli routines.
Created on April 4, 2012"}
(:require [clojure.string :as cstr]
[clojure.data.csv :as csv]
[clojure.java.io :as io])
(:import java.util.Date)
(:import java.text.SimpleDateFormat)
(:import java.text.ParseException)
(:import java.io.File)
(:use clojure-csv.core))
Here is the Java code
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
GregorianCalendar gc = new GregorianCalendar();
java.util.Date d = sdf.parse("12/12/2003");
Here is parse call in lein repl
user=> (def x1 (SimpleDateFormat. "yyyy-MM-dd"))
#'user/x1
user=> (.parse x1 "2014-08-06")
#inst "2014-08-05T21:00:00.000-00:00"
user=> (type (.parse x1 "2014-08-06"))
java.util.Date
read http://clojure.org/java_interop to know how to translate java code.

How to get the current date format of Android emulator

I want to get the current Date Format of Android emulator. Can anyone help me?
Not like this
SimpleDateFormat FormattedDATE = new SimpleDateFormat("M-d-yyyy");
Calendar cal = Calendar.getInstance();
There are various options:
DateFormat defaultFormat = DateFormat.getDateInstance();
DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
// etc
And likewise for getDateTimeInstance.
Basically look at the static methods of DateFormat that return instances of DateFormat.
java.time
The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time, the modern Date-Time API:
DateTimeFormatter#ofLocalizedDate provides a formatter that uses the locale-specific date format.
Demo:
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now(ZoneId.of("America/New_York"));
DateTimeFormatter shortDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.localizedBy(Locale.ENGLISH);
System.out.println(shortDateFormatter.format(today));
DateTimeFormatter mediumDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.localizedBy(Locale.ENGLISH);
System.out.println(mediumDateFormatter.format(today));
DateTimeFormatter longDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
.localizedBy(Locale.ENGLISH);
System.out.println(longDateFormatter.format(today));
}
}
Output:
7/13/21
Jul 13, 2021
July 13, 2021
ONLINE DEMO
Change the ZoneId and the Locale as applicable.
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Categories

Resources