简体   繁体   中英

Convert system time as string to Timestamp

I'm trying to get the time of my computer using Java and format it in specific order using SimpleDateFormat object but it won't format; please help.

This is my code :

java.util.Date parsedDate = null;      
try {   
    DateFormat dateFormat = new SimpleDateFormat("HH:mm");  
    parsedDate = dateFormat.parse(dateFormat.format(new java.util.Date()));
}catch(ParseException e){
}  
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());

tl;dr

LocalTime
    .now( ZoneId.of( "Pacific/Auckland" ) ) 
    .toString() 

java.time

You are using troublesome troublesome old legacy classes that are now supplanted by the modern java.time classes. Avoid the old classes entirely.

Get the current moment in UTC with a resolution of up to nanoseconds.

Instant instant = Instant.now() ;

To see that same moment through a particular region's wall-clock time, apply a time zone.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atone( z ) ;

To work with only a time-of-day without a date and without a time zone, extract a LocalTime object.

LocalTime lt = zdt.toLocalTime() ;

If by "format to specific order" you meant sorting… These objects know how to sort. No need for a string just for sorting.

List<LocalTime> times = new ArrayList<>() ;
times.add( lt ) ;
…
Collections.sort( times ) ;

Generate a String for display to user. The java.time classes use standard ISO 8601 formats by default for generating/parsing strings.

String output = lt.toString() ;

For other formats, use DateTimeFormatter .

DateTimeFormatter f = DateTimeFormatter.ofPattern( "HH:mm" ) ;
String output = lt.format( f ) ;

All of this has been handled many times on Stack Overflow. Search for more info and discussion.

Hi user8545027 i modified you code a little bit and it works for me now. I also used ThreadLocal to make SimpleDateFormat safe for MultiThreading.

package com.java;

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

public class Test {

    private ThreadLocal<SimpleDateFormat> threadSafeDateFormat = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("HH:mm");
        }
    };

    String formatDate() {
        SimpleDateFormat format = threadSafeDateFormat.get();
        String timeStamp = format.format(new Date());
        return timeStamp;
    }


    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test.formatDate());
    }
}

Let me know if any questions. Hope this Helps.

Use:

public class Basics {

  public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("HH:mm");
    String date = dateFormat.format(System.currentTimeMillis());
    System.out.println(date);   
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM