Change time in genymotion emulator – simple java app

I made app in executable jar java file for setting time in genymotion emulator.

Here is simple java code and compiled app for setting time in genymotion app. Below code I showed how the app looks like and also added ready jar app to download.

Code:

public class SetTimeInGenyMotion {
    private JTextField textField1;
    private JButton getTimeInProperButton;
    private JButton setTimeInProperButton;
    private JButton getTimeAndSetButton;
    private JPanel panel1;
    public static final String FORMAT = "YYYYMMDD.HHmmss";

    public static void main(String[] args) {
        new SetTimeInGenyMotion().doStuff();
    }

    private void doStuff() {

        getTimeInProperButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField1.setText(getTime());
            }
        });

        setTimeInProperButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String time = textField1.getText();
                setTime(time);
            }
        });

        getTimeAndSetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String time = getTime();
                setTime(time);
                textField1.setText(time);
            }
        });

        showWindow();
    }

    private void showWindow() {
        JFrame frame = new JFrame("SetTimeInGenyMotion");
        frame.setContentPane(panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(680, 0));
        frame.pack();
        frame.setVisible(true);
    }

    private void setTime(String time) {
        try {
            String command = "adb shell date -s " + time;

            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec(command);
            BufferedReader br = new BufferedReader(new InputStreamReader(pr.getErrorStream(), "UTF-8"));
            String l = "";

            while ((l = br.readLine()) != null) {
                System.out.println(l);
            }
            br.close();

        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    private String getTime() {
        //adb shell date -s YYYYMMDD.HHmmss
        //http://stackoverflow.com/questions/7094772/how-to-set-android-device-date-time-programmatically

        String format = "YYYYMMdd.HHmmss";
        Calendar instance = Calendar.getInstance();
        String time = getDate(instance.getTimeInMillis(), format);
        return time;
    }

    public static String getDate(long milliSeconds, String dateFormat) {
        // Create a DateFormatter object for displaying date in specified format.
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        // Create a calendar object that will convert the date and time value in milliseconds to date.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);
        return formatter.format(calendar.getTime());
    }
}

Download:
download

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :