Jeremiah Me
Jeremiah Me

Reputation: 153

Set Current Time in android

public void setCurrenttime () {

    c = Calendar.getInstance(); 
    hour = c.get(Calendar.HOUR_OF_DAY);
    min = c.get(Calendar.MINUTE);
    String am_pm;
    if (hour > 12) {              
        am_pm = "PM";
    } else {
        am_pm = "AM";
    }

    tvDisplayHour=(TextView)findViewById(R.id.hourtext);   
    tvDisplayHour.setText(hour+":"+ min + " "+ am_pm );     
}

My problem is it wont set the Current time in my emulator.

What's wrong with that code? please help I'm new in android .

Thanks


public void setCurrenttime () {

    c = Calendar.getInstance(); 
    hour = c.get(Calendar.HOUR_OF_DAY);
    min = c.get(Calendar.MINUTE);
    String am_pm;
    if (hour > 12) {              
        am_pm = "PM";
    } else {
        am_pm = "AM";
    }

    tvDisplayHour=(TextView)findViewById(R.id.hourtext);   
    tvDisplayHour.setText(hour+":"+ min + " "+ am_pm );     
}

In this code how I can convert into 12 hour format?

Upvotes: 1

Views: 802

Answers (2)

Namrata
Namrata

Reputation: 1684

Try this: You will get current time:-

Modify it with your requirements:-

 public void current_time() {

        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int am_pm = cal.get(Calendar.AM_PM);

        TimePickerDialog mTimePicker;
        mTimePicker = new TimePickerDialog(InformationActivity.this,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker,
                            int selectedHour, int selectedMinute) {

                        String minutes = "";

                        int startTimeHour = selectedHour;
                        int startTimeMinute = selectedMinute;

                        if (selectedMinute < 10) {
                            minutes = "0" + selectedMinute;
                        } else {
                            minutes = String.valueOf(selectedMinute);
                        }

                        if (selectedHour > 12) {
                            selectedHour -= 12;

                            String timeSet_in = "PM";
                            edt_time_in.setText(selectedHour + ":" + minutes
                                    + " " + timeSet_in.toString());
                            selectedHour += 12;

                        } else if (selectedHour == 0) {

                            selectedHour += 12;

                            timeSet_in = "AM";
                            edt_time_in.setText(selectedHour + ":" + minutes
                                    + " " + timeSet_in.toString());
                            selectedHour -= 12;
                        } else if (selectedHour == 12) {

                            timeSet_in = "PM";
                            edt_time_in.setText(selectedHour + ":" + minutes
                                    + " " + timeSet_in.toString());
                        } else {

                            timeSet_in = "AM";
                            edt_time_in.setText(selectedHour + ":" + minutes
                                    + " " + timeSet_in.toString());
                        }

                        arriveLoadHour = selectedHour;
                        arriveLoadMinute = selectedMinute;

                    }
                }, hour, minute, true);// Yes 24 hour time
        mTimePicker.setTitle("Select Time");
        mTimePicker.show();

    }

Upvotes: 0

user3301551
user3301551

Reputation: 350

The code you are using is to fetch the time from system not to set the time to the system. If you want to set time to System then you need to use AlarmManager class. Check following code,

Calendar c = Calendar.getInstance();
c.set(2014, 8, 15, 12, 34, 56);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setTime(c.getTimeInMillis());

Also you need to define following permissions into your AndroidManifest.xml file

<permission android:name="android.permission.SET_TIME"
    android:protectionLevel="signature|system"
    android:label="@string/permlab_setTime"
    android:description="@string/permdesc_setTime" />

Upvotes: 2

Related Questions