简体   繁体   中英

How to get current month in webdriver using java

I wonder if someone can help!

I've been using following 'Get_Date' class with method 'Get_month' to return the current month but I've now decided to move onto webdriver but am not sure what the webdriver equivalent of following function be.

package hisScripts;
import com.thoughtworks.selenium.*;

public class Get_Date {

public String Get_Month(Selenium selenium) {
    return selenium.getEval("var MonthNames =  [ \"Jan 2012\", \"Feb 2012\", \"Mar 2012\", \"Apr 2012\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Nov 2011\", \"Nov 2011\", \"Dec 2011\" ];d = new Date();MonthNames[d.getMonth()];");
   }
}

Try this:

 public String get_Month(){
   Date date = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("MMMMMMMMM");
   return formatter.format(date);          
 }

In case of this month it will return April

If you need other notations, refer to SimpleDateFormat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM