Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to date and time as am/pm format [duplicate]

 string : 2014-04-25 17:03:13

using SimpleDateFormat is enough to format? or otherwise i will shift to any new API?

Date date = new Date(string);
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
out.println( dateFormat.format (date));

My expected result is (India zone):

Date : 25-04-2014
Time : 05:03 PM
like image 809
Developer Avatar asked May 20 '14 04:05

Developer


People also ask

How do you convert date to time AM or PM?

From 0:00 (midnight) to 0:59, add 12 hours and use am. From 1:00 to 11:59, just add am after the time. From 12:00 to 12:59, just add pm after the time. From 13:00 to 0:00, subtract 12 hours and use pm.

How do I display AM PM in date format?

There are two patterns that we can use in SimpleDateFormat to display time. Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM. aa – AM/PM marker. In this example we are displaying current date and time with AM/PM marker.

Can we convert string to timestamp?

To convert a date string to a timestamp: Pass the date string to the Date() constructor. Call the getTime() method on the Date object. The getTime method returns the number of milliseconds since the Unix Epoch.

How do you convert string to time?

You can use the following code for changing the String value into the time equivalent: String str = "08:03:10 pm"; DateFormat formatter = new SimpleDateFormat("hh:mm:ss a"); Date date = (Date)formatter. parse(str); Hope this helps you.


3 Answers

Remembering that Date objects have no inherent format, you need two DateFormat objects to produce the result you seek - one to parse and another to format:

String input = "2014-04-25 17:03:13";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("'Date : 'dd-MM-yyyy\n'Time : 'KK:mm a");
System.out.println(outputFormat.format(inputFormat.parse(input)));

Output:

Date : 25-04-2014
Time : 05:03 PM

Note the use of quoted sequences in the format, such a "'Date : '", which is treated as a literal within the format pattern.

like image 199
Bohemian Avatar answered Sep 20 '22 00:09

Bohemian


I custom onTimeSet() function . Send the hour and minutes to it. It will return the time with format am and pm

public static String onTimeSet( int hour, int minute) {

    Calendar mCalen = Calendar.getInstance();;
       mCalen.set(Calendar.HOUR_OF_DAY, hour);
       mCalen.set(Calendar.MINUTE, minute);

       int hour12format_local = mCalen.get(Calendar.HOUR);
       int hourOfDay_local = mCalen.get(Calendar.HOUR_OF_DAY);
       int minute_local = mCalen.get(Calendar.MINUTE);
       int ampm = mCalen.get(Calendar.AM_PM);
       String minute1;
       if(minute_local<10){

        minute1="0"+minute_local;
       }
       else
          minute1=""+minute_local;

       String ampmStr = (ampm == 0) ? "AM" : "PM";
       // Set the Time String in Button

       if(hour12format_local==0)
        hour12format_local=12;



String selecteTime=hour12format_local+":"+ minute1+" "+ampmStr;

retrun selecteTime;


}
like image 40
Pawan asati Avatar answered Sep 22 '22 00:09

Pawan asati


DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");

more patterns you can find here

like image 25
hashplus Avatar answered Sep 22 '22 00:09

hashplus