-3

How to display only time in UNIX? I only using java6. Can't upgrade the java cause have some inconvience cause.

 DateFormat dfm = new SimpleDateFormat("yyyymmdd");     
 Calendar calConvert = Calendar.getInstance();
 String currDate = dateFormat.format(calConvert);
 int year = Integer.parseInt(currDate .substring(0, 4));
 int month = Integer.parseInt(currDate .substring(4, 6));
 int day = Integer.parseInt(currDate .substring(6));
 int hour = Integer.parseInt(candle2.sTransTimeTo.substring(0, 2));
 int minute = Integer.parseInt(candle2.sTransTimeTo.substring(2, 4));
 int second = Integer.parseInt(candle2.sTransTimeTo.substring(4));

 calConvert.setTimeZone(TimeZone.getTimeZone("GMT + 8:00"));
 calConvert.set(year, month, day);
 calConvert.set(Calendar.HOUR, hour);
 calConvert.set(Calendar.MINUTE, minute);
 calConvert.set(Calendar.SECOND, second); 
 candle2.sTransTimeTo = Long.toString(calConvert.getTimeInMillis() / 1000);

candle2.sTransTimeTo is the time we need to display. How to show only time?

if i do as below coding:

calConvert.setTimeZone(TimeZone.getTimeZone("GMT + 8:00"));    
calConvert.set(Calendar.HOUR, hour);
calConvert.set(Calendar.MINUTE, minute);
calConvert.set(Calendar.SECOND, second); 
candle2.sTransTimeTo = Long.toString(calConvert.getTimeInMillis() / 1000);
Sharon Wong
  • 109
  • 1
  • 2
  • 14
  • 2
    What you are doing in that code seems way overcomplicated. Why not just do `int year = calConvert.get(Calendar.YEAR);` instead of first formatting it into a string and then parsing parts of the string again? Also, it's not clear what you are asking. What result exactly do you expect? – Jesper Aug 03 '17 at 11:22
  • my expectation is i only wan show Time without date in UNIX. – Sharon Wong Aug 03 '17 at 11:25
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). For Java 6 & 7 see the ThreeTen-Backport project. – Basil Bourque Aug 03 '17 at 18:17
  • @SharonWong (a) `org.threeten.bp.Instant.now().getEpochSecond()` (b) Search Stack Overflow thoroughly before posting. – Basil Bourque Aug 03 '17 at 18:43

4 Answers4

1

Please read the official Java documentation. But for now the code below will give you a head start.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormattedDateTimeOnly {

    public static void main(String[] args){
        DateFormat yourDateFormat = new SimpleDateFormat("hh:mm:ss a");
        Date date = new Date();
        String time = yourDateFormat.format(date);
        System.out.println(time);
    }
}
royjavelosa
  • 2,048
  • 6
  • 30
  • 46
0

Have you tried this?

//instantiates a calendar using the current time in the specified timezone
Calendar cSchedStartCal=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
//change the timezone
cSchedStartCal.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));

From the question How to change TIMEZONE for a java.util.Calendar/Date

A1t0r
  • 469
  • 5
  • 26
0

To display the current local time you can simply do this:

DateFormat dateFormat = new SimpleDateFormat("HH:mm");
String text = dateFormat.format(new Date());

System.out.println(text);
Jesper
  • 202,709
  • 46
  • 318
  • 350
0

tl;dr

To get count of whole seconds since the Unix epoch of 1970-01-01T00:00:00Z.

Instant.now()
       .getEpochSecond() 

Details

This has been covered many times already on Stack Overflow, so briefly…

Use modern java.time classes, not the troublesome legacy Date/Calendar classes.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now() ; 

Use true time zones when known, rather the mere offset-from-UTC.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

Assign a ZoneId to get a ZonedDateTime.

ZoneId z = ZoneId.of( "Asia/Brunei" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

To extract only the date, without a time-of-day and without a time zone, ask for a LocalDate.

LocalDate ld = zdt.toLocalDate() ;

To extract only the time-of-day, convert to a LocalTime.

LocalTime lt = zdt.toLocalTime() ;

To generate strings in standard ISO 8601 format, call toString on any java.time object. For other formats, see DateTimeFormatter and DateTimeFormatterBuilder.

➠ To get the number of seconds from the epoch reference date of 1970-01-01T00:00Z, interrogate the ZonedDateTime. Some call this Unix Time.

long secondsFromEpoch = zdt.toEpochSecond() ;

To get the number of seconds from midnight, use Duration. Do not assume midnight is 00:00:00. Anomalies such as Daylight Saving Time (DST) mean the day might start at a time such as 01:00:00. Ask java.time to determine the first moment of the day.

long secondsSinceStartOfDay = 
    Duration.between( 
        zdt.atStartOfDay( z ) ,  // Get first moment of the day, may or may not be `00:00:00`.
        zdt 
    ).getSeconds() ;             // Render duration as a total number of seconds.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154