There is a "Pretty time" library for Java based on this post:
How to calculate "time ago" in Java?
Is there anything like this for GWT?
GWT relative time
I will not recommend you to include some 3-rd parties for this task - there is more simple way.
Just count number of seconds, minutes, hours, ...e.t.c. and then format the result text - use Plural Forms - built in GWT i18n tool for formatting such text as
"one second", "two seconds", e.t.c. So all messages will be localized and stored into i18n resources, no any hardcode.
I coded this for an Android java project. I created a class with 2 methods:
/**
* Created by mihai on 2/27/17.
*/
import android.util.Log;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.ocpsoft.prettytime.PrettyTime;
public class Ptime {
static public class PtimeFormatter {
public static String getPtime(){
PrettyTime p = new PrettyTime();
Log.d("demo", "time now: "+new Date());
//getPtimeFrom("Mon Feb 27 19:17:13 EST 2017");
return p.format(new Date());
//prints: “moments from now”
//System.out.println(p.format(new Date(System.currentTimeMillis() + 1000 * 60 * 10)));
//prints: “10 minutes from now”
}
public static String getPtimeFrom(String t){
PrettyTime p = new PrettyTime();
DateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
try {
Date date = (Date)formatter.parse(t);
Log.d("demo", "time from now: "+p.format(date));
return p.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
//Log.d("demo", "time now: "+p.format(date));
return null;
}
public static String getPTimeMillis(String t){
PrettyTime p = new PrettyTime();
String currMilis = String.valueOf(System.currentTimeMillis());
DateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
try {
Date aTime = formatter.parse(t);
Log.d("demo", "time millis: "+aTime.getTime());
return String.valueOf(Long.parseLong(currMilis) - aTime.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
}
Related
Facing error message when we defined the web driver factory in groovy.
Are there any errors in my code?
Code Snippet:
private static WebDriver driver=null;
#Keyword
public static void Customized_Start_Time()
{
driver = DriverFactory.getWebDriver();
Date date = new Date();
Date yesterday = date.previous()
SimpleDateFormat customDate;
customDate = new SimpleDateFormat("d MMM yy"); // Date format could be 03-Sep-20
String dateOutput = customDate.format(yesterday);
System.out.println(dateOutput);
//Date Format is 03-Sep-20
String[] dateParts=dateOutput.split(" ")
String res=dateParts[0]
println dateParts[0]
String beforeXpath="//table[#uitestid='gwt-debug-customFromDatePicker']/tbody/tr[2]/td/table[#class='datePickerDays']/tbody/tr[";
String AfterXpath="]/td[";
String LastXpath="]/div"
boolean flag=false;
for(int rowNum=2; rowNum<=7;rowNum++)
{
for(int colNum=2;colNum<=7;colNum++)
{
String dateval=driver.findElement(By.xpath("beforeXpath+rowNum+AfterXpath+colNum+LastXpath")).getText()
//String dateval =WebUI.getText(findTestObject('beforeXpath+rowNum+AfterXpath+colNum+LastXpath'), FailureHandling.OPTIONAL)
println (dateval)
if (dateval.equals(res))
{
driver.findElement(By.xpath("beforeXpath+rowNum+AfterXpath+colNum+LastXpath")).click()
flag=true;
break;
}
}
if(flag)
{
break;
}
}
You need to import Selenium's By library.
Add the following to the top of your script (where the other imports are):
import org.openqa.selenium.By
Or, you can automatically add the missing imports by pressing Ctrl + Shift + O.
I have an input string like :
billDate="2016-03-16T10:48:59+05:30" (please see the T in between).
Now I want to convert this to another timestamp (America/New_York).
My final result should be like 16 march 2016 or 15th march 2016 depending upon the hour value.
I saw many examples but got no hint how I can convert the above long datetime string to another string for America/New_York.
Can somebody help me on this?
I tried below code but it always gives 16 march for any hour value.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Test {
public static void main(String[] args) {
String output = formatDate("2016-03-1611T:27:58+05:30");
System.out.println(output);
}
public static String formatDate(String inputDate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
Date parsedDate = sdf.parse(inputDate);
return sdf.format(parsedDate);
}
catch (ParseException e) {
// handle exception
}
return null;
}
}
After trying I finally got the code to solve the issue:
The below code works fine:
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
public class Test {
public static final SimpleDateFormat fDateTime = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss");
public static void main(String[] args) {
String output = getFormattedDate("2016-03-1611T23:27:58+05:30");
System.out.println(output);
}
public static String getFormattedDate(String inputDate) {
try {
Date dateAfterParsing = fDateTime.parse(inputDate);
fDateTime.setTimeZone(TimeZone.getTimeZone("timeZone"));
return fDateTime.format(dateAfterParsing);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
I am working on a gwt application which involves advanced manipulations with date times: convert from one timezone to another, etc. Gwt has some low level stuff for working with dates but they are too low level for me. Are there any options similar to joda time or threeten for gwt?
You could look at the following options.
http://code.google.com/p/gwt-time/
http://code.google.com/p/goda-time/
http://github.com/mping/gwt-joda-time
This is my DateTimeUtil class
public class DateTimeUtil {
public static String getYear(Date date) {
return DateTimeFormat.getFormat("yyyy").format(date);
}
public static String getMonth(Date date) {
return DateTimeFormat.getFormat("MM").format(date);
}
public static String getDay(Date date) {
return DateTimeFormat.getFormat("dd").format(date);
}
public static String getHour(Date date) {
return DateTimeFormat.getFormat("HH").format(date);
}
public static String getMinute(Date date) {
return DateTimeFormat.getFormat("mm").format(date);
}
public static String getSecond(Date date) {
return DateTimeFormat.getFormat("ss").format(date);
}
// The String year must to have yyyy format
public static Date getDate(String years, String months, String days, String hours, String minutes, String seconds) {
DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
Date date = dtf.parse(years + "-" + months + "-" + days + " " + hours + ":" + minutes + ":" + seconds);
GWT.log("date parsed " + date);
return date;
}
}
I am trying to convert string into date type object but keep getting class cast exception.I checked everywhere and found the same way as I am using .I have no idea what mistake I am committing.kindly help.
String str;
SimpleDateFormat formatter;
Date date;
str="12/23/2011"
formatter=new SimpleDateFormat("MM/dd/yyyy");
try {
date=(Date)formatter.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This code works fine with me, maybe you made an error with the imports.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
String str = "12/23/2011";
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = formatter.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date);
}
}
how to get current date in DD-MM-YYYY format in BlackBerry
i have already tried the following, but it gives me output of 1318502493
long currentTime = System.currentTimeMillis() / 1000;
System.out.println("Current time in :" + currentTime);
private String pattern = "dd-MM-yyyy";
String dateInString =new SimpleDateFormat(pattern).format(new Date());
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
return formatter.format(new Date());
Check if you can use SimpleDateFormat. If you can, create an object of this class, and use it in order to format the return provided by System.currentTimeMillis(). Some code below:
import java.util.*;
import java.text.*;
public class DateTest {
public static String getCurrentTimeStamp() {
SimpleDateFormat formDate = new SimpleDateFormat("dd-MM-yyyy");
// String strDate = formDate.format(System.currentTimeMillis()); // option 1
String strDate = formDate.format(new Date()); // option 2
return strDate;
}
public static void main (String args[]) {
System.out.println(getCurrentTimeStamp());
}
}