Question

I have simple Java program and I want to get just time and get in it. Please, help me.

package javaapplication1;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class JavaApplication1
{
  public static void main(String[] args)
   {
        Date trailDate=new Date();
        GregorianCalendar calendar1 = new GregorianCalendar();
        calendar1.setTime(trailDate);
        try 
        {
        XMLGregorianCalendar 
        date1=DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar1);
        System.out.println(date1);
        }
       catch (DatatypeConfigurationException ex)
        {
        Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

I GET folowing output.

  2013-08-30T17:01:35.446+05:30

but I want only below output

  2013-08-30T17:01:35
Was it helpful?

Solution

Get the date from the calendar and use SimpleDateFormat to format it into a String.

OTHER TIPS

try this

System.out.println(date1.toString().replaceAll("(.*)\\..*", "$1"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top