4

I need to create a field with time only. So, I had created a Visualforce Page and a custom controller that will work with only Html 5.0.

Once the user try to create any account I am roting my Visulaforce Page with capturing current time of the user working perfectly fine. After user saves the record I update the time in a custom text field and display in normal standard page. Everything works fine.

But after user saves the record it displays like:

Time 19:34:00.000Z

But I need

7:38 AM/PM.

My Apex class

public class MyTimeController {

   public Account acc{get;set;}
   public String var{get;set;}
   public Boolean edit{get;set;}
    public MyTimeController(ApexPages.StandardController controller) {

    var=ApexPages.currentPage().getParameters().get('Id'); 
     if(var!=null){

     acc=[Select Id,Name,Time__c from Account where Id=:var];
     edit=true;
     }
     else{
     edit=false;
     acc=new Account();
     }    

    }


    public PageReference save() {
       acc.Time__c=String.ValueOf(myTime);
       if(!edit){
       insert acc;
       }
       else{
       update acc;
       }
       PageReference redirectPage =  new PageReference('/'+acc.id);
       redirectPage.setRedirect(true);
       return redirectPage;
       return null;
    }


    public Time myTime { 
        get {
            if (myTime == null) {
                myTime =  DateTime.now().Time();

            }
            return myTime;
        } 
        set;
    }
}

1 Answer 1

10

Instead of

acc.Time__c=String.ValueOf(myTime);

Use:

Datetime myDateTime = Datetime.newInstance(Date.today(), myTime);
acc.Time__c = myDateTime.format('h:mm a');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .