Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problem to get hour and minute

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem to get hour and minute

    Hello,

    I am creating an Android application, I inserted a textfields "time" that allows the user to choose a time in the format "hh: mm" (is it also possible to change it in the format "mm: ss "?), and I want to get this hour (and minute) to convert it to milliseconds but I do not know how I can do it ?

    Thanks for your help !


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem to get hour and minute

    to convert it to milliseconds
    Are the milliseconds you are asking about the count of time since the beginning of the epoch? About Jan 1, 1970

    Does Android have the same classes as Java SE for parsing Strings and creating Date objects?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem to get hour and minute

    Yes, it's the number of milliseconds since the beginning of the Jan 1, 1970. And yes, Android have the same classes as JAVA but I don't know how I can get the number of hours and minutes that the user has chosen. Because the user choose a time in this format "hh:mm"

    [code=Java]<EditText
    android:id="@+id/temps"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:ems="10"
    android:inputType="time" />[code]

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem to get hour and minute

    There are classes and methods for parsing those Strings into a Date object.
    Where do the year, month and day come from?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem to get hour and minute

    The problem is there is no year, no month and no day but just a time which is in the Edittext.

    --- Update ---

    But I think I can use the current day, month and day.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem to get hour and minute

    Is this what you are trying to do:
    Given a String: "11:23" you want to use today's date: (year, month and day) and that given hour and minutes with seconds = 0 to compute the milliseconds?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem to get hour and minute

    Yes, it's is exactly that I want to do.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem to get hour and minute

    Try writing the code in Java SE first as it's easier to work with. When that works, copy it to the Android code.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem to get hour and minute

    Can I do something like that ? I don't know how put the hour and minutes that I want ?

     Calendar c = Calendar.getInstance();
    	    		c.set(Calendar.SECOND, 0);
    	    		c.set(Calendar.HOUR, ? );
    	    		c.set(Calendar.MINUTE, ?);
    	    		c.getTimeInMillis();

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem to get hour and minute

    What happens when you compile and execute that code?
    Replace the ? with good values.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. URGENT! Due in one hour. Please help!
    By imanoob in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 10th, 2013, 11:25 AM
  2. Rush hour game
    By zerocos in forum What's Wrong With My Code?
    Replies: 46
    Last Post: January 25th, 2013, 06:05 PM
  3. [SOLVED] JAVA last minute homework, CAN'T DO A LOOP please help.
    By lauren_freeman in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 26th, 2012, 11:38 PM
  4. Help with Clock (advance and reverse day,hour,minute)
    By whyld4900 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 13th, 2011, 02:39 AM