1

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
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
Java Man
  • 1,854
  • 3
  • 21
  • 43

2 Answers2

1

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

Eric Stein
  • 13,209
  • 3
  • 37
  • 52
  • we will you explain me how to do this? – Java Man Aug 30 '13 at 11:37
  • @kapil which part are you having trouble with? Getting the date from the calendar or using SimpleDateFormat? Have you looked at the javadoc? – Eric Stein Aug 30 '13 at 11:39
  • using SimpleDateFormat – Java Man Aug 30 '13 at 11:40
  • @kapil Why don't you try to write some code using SimpleDateFormat and let us know how it goes? The javadoc is very good, and there are a ton of examples out there. – Eric Stein Aug 30 '13 at 11:42
  • i use SimpledareFormate but i got String Object now how to pass in to..GregorianCalendar as a SetTime() method? – Java Man Aug 30 '13 at 11:56
  • @kapil Objects like GregorianCalendar don't have a format. Only Strings have a format. You said you wanted to print out the date in a different format, but apparently that's not true. What are you really trying to do? – Eric Stein Aug 30 '13 at 12:01
  • i want date and time in XMLGregorianCalendar .tell me please how to do this.. i have one method that will accept only XMLGregorianCalendar value. so how to get date and time using XMLGregorianCalendar. – Java Man Aug 30 '13 at 12:11
0

try this

System.out.println(date1.toString().replaceAll("(.*)\\..*", "$1"));
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275