shendongshia
8/2/2018 - 6:41 AM

TimeZone SimpleDateFormat ZonedDateTime ZoneId

        var backUpFileData = [];
        for (var i = 0; i < 30; i++)
        {
            backUpFileData.push("20180812135035.zip");
        }
        var tableObject = jQuery("#backUpFileTable");
        var rowTotalNum = backUpFileData.length % 6 === 0 ? Math.floor(backUpFileData.length / 6) : Math.ceil(backUpFileData.length / 6);
        var tableHtml = '';
        for (var rowNum = 0; rowNum < rowTotalNum; rowNum++)
        {
            tableHtml = tableHtml + '<tr>';
            for (var colNum = 0; colNum < 9; colNum++)
            {
                if (rowNum * 8 + colNum < backUpFileData.length && rowNum === 0)
                {
                    var ceilData = backUpFileData[rowNum * 6 + colNum];
                    ceilDataHtml = '<span ng-click = "downloadBackupFile(' + ceilData.substring(0, ceilData.length - 4) + ')" class = "cusorHandWithHight">' + ceilData + '</span> <img src="/devices-app/themes/default/eview_modules/configBackup/backupFile/1.0.0/template/images/g_del.png" class = "cusorHandWithoutColor" ng-click = "deleteBackupFile(' + ceilData.substring(0, ceilData.length - 4) + ')"></img>'
                    tableHtml = tableHtml + '<td>' + ceilDataHtml + '</td>';
                }
                else if (rowNum * 6 + colNum < backUpFileData.length && rowNum !== 0)
                {
                    var ceilData = backUpFileData[rowNum * 6 + colNum];
                    ceilDataHtml = '<span ng-click = "downloadBackupFile(' + ceilData.substring(0, ceilData.length - 4) + ')" class = "cusorHandWithHight">' + ceilData + '</span> <img src="/devices-app/themes/default/eview_modules/configBackup/backupFile/1.0.0/template/images/g_del.png"  class = "cusorHandWithoutColor" ng-click = "deleteBackupFile(' + ceilData.substring(0, ceilData.length - 4) + ')"></img>'
                    tableHtml = tableHtml + '<td>' + ceilDataHtml + '</td>';
                }
            }
            tableHtml = tableHtml + '</tr>'
        }
        if (backUpFileData.length !== 0)
        {
            angular.element(document).injector().invoke(function ($compile)
            {
                var finalHtml = $compile(tableHtml)($scope);
                jQuery("#backUpFileTable").html(finalHtml)
            });
        }

        $scope.downloadBackupFile = function (backupFileName)
        {
            console.log("downloadevent is:", backupFileName);
        }
        $scope.deleteBackupFile = function (backupFileName)
        {
            console.log("deleteevent is:", backupFileName);
        }
public static String transSpecialChar(String input) { 
  if (null != input && 0 != input.length()) { 
    if (input.contains("_")) { input = input.replaceAll("", "\\_"); } 
      if (input.contains("%")) { input = input.replaceAll("%", "\\%"); } 
      if (input.contains("'")) { input = input.replaceAll("'", "''"); } 
    
  } return input; }
  
  public static enum SynTaskType {
        DEVICE("device"),
        VSYS("vsys"),
        DEVICE_FIRST("device_first"),
        VSYS_FIRST("vsys_first"),
        DEVICE_CHECK("device_check"),
        DEVICE_GROUP("device_group"),
        VSYS_GROUP("vsys_group"),
        UPDATE_VSYS_GROUP("update_vsys_group");

        private final String synTaskType;

        private SynTaskType(String synTaskType) {
            this.synTaskType = synTaskType;
        }

        public String getSynTaskType() {
            return this.synTaskType;
        }
    }
public static final String CERT_PATH = Paths.get(File.separator, "opt", "controller", "naas", "naas-karaf-1.0.1-SNAPSHOT", "upgrade", "cert").toString();
-- Table: TAB_SYNC_ECS_OPERSTATUSENTITY

-- DROP TABLE TAB_SYNC_ECS_OPERSTATUSENTITY;

CREATE TABLE TAB_SYNC_ECS_OPERSTATUSENTITY
(
  JSDOC JSONB,
  CONSTRAINT TAB_SYNC_ECS_OPERSTATUSENTITY_ID_NOT_NULL CHECK ((JSDOC ->> '_id'::TEXT) IS NOT NULL)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE TAB_SYNC_ECS_OPERSTATUSENTITY OWNER TO HWAC;
GRANT ALL ON TABLE TAB_SYNC_ECS_OPERSTATUSENTITY TO HWAC;
GRANT SELECT ON TABLE TAB_SYNC_ECS_OPERSTATUSENTITY TO HWACSHELL;

-- Index: TAB_SYNC_ECS_OPERSTATUSENTITY_DEVICE_ID_INDEX
-- DROP INDEX TAB_SYNC_ECS_OPERSTATUSENTITY_DEVICE_ID_INDEX;

CREATE UNIQUE INDEX TAB_SYNC_ECS_OPERSTATUSENTITY_DEVICE_ID_INDEX  ON TAB_SYNC_ECS_OPERSTATUSENTITY USING BTREE((JSDOC ->> 'deviceId'::TEXT) COLLATE PG_CATALOG."default");


1,查看数据库
    playboy=> \l                       //\加上字母l,相当于mysql的,mysql> show databases;  
            List of databases  
       Name    |  Owner   | Encoding  
    -----------+----------+----------  
     playboy   | postgres | UTF8  
     postgres  | postgres | UTF8  
     template0 | postgres | UTF8  
     template1 | postgres | UTF8  
      
    playboy=> select pg_database_size('playboy');    //查看playboy数据库的大小  
     pg_database_size  
    ------------------  
              3637896  
    (1 row)  
      
    playboy=> select pg_database.datname, pg_database_size(pg_database.datname) AS size from pg_database;    //查看所有数据库的大小  
      datname  |  size  
    -----------+---------  
     postgres  | 3621512  
     playboy   | 3637896  
     template1 | 3563524  
     template0 | 3563524  
    (4 rows)  
      
    playboy=> select pg_size_pretty(pg_database_size('playboy'));      //以KB,MB,GB的方式来查看数据库大小  
     pg_size_pretty  
    ----------------  
     3553 kB  
    (1 row)
    
  
  2,查看多表
  
    playboy=> \dt                      //相当于mysql的,mysql> show tables;  
            List of relations  
     Schema | Name | Type  |  Owner  
    --------+------+-------+---------  
     public | test | table | playboy  
    (1 row)  
    
  3,查看单表
      playboy=> \d test;                 //相当于mysql的,mysql> desc test;  
                Table "public.test"  
     Column |         Type          | Modifiers  
    --------+-----------------------+-----------  
     id     | integer               | not null  
     name   | character varying(32) |  
    Indexes: "playboy_id_pk" PRIMARY KEY, btree (id)  
      
    playboy=> select pg_relation_size('test');   //查看表大小  
     pg_relation_size  
    ------------------  
                    0  
    (1 row)  
      
    playboy=> select pg_size_pretty(pg_relation_size('test'));   //以KB,MB,GB的方式来查看表大小  
     pg_size_pretty  
    ----------------  
     0 bytes  
    (1 row)  
      
    playboy=> select pg_size_pretty(pg_total_relation_size('test'));   //查看表的总大小,包括索引大小  
     pg_size_pretty  
    ----------------  
     8192 bytes  
    (1 row)  
    
  4,查看索引
      playboy=> \di                      //相当于mysql的,mysql> show index from test;  
                    List of relations  
     Schema |     Name      | Type  |  Owner  | Table  
    --------+---------------+-------+---------+-------  
     public | playboy_id_pk | index | playboy | test  
    (1 row)  
      
    playboy=> select pg_size_pretty(pg_relation_size('playboy_id_pk'));    //查看索大小  
     pg_size_pretty  
    ----------------  
     8192 bytes  
    (1 row)  
    
  5,查看表空间,以及大小
      playboy=> select spcname from pg_tablespace;         //查看所有表空间  
      spcname  
    ------------  
     pg_default  
     pg_global  
    (2 rows)  
      
    playboy=> select pg_size_pretty(pg_tablespace_size('pg_default'));   //查看表空间大小  
     pg_size_pretty  
    ----------------  
     14 MB  
    (1 row)  
  
    
LocalDate lastDayOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());

@Test
public void whenAdjust_thenNextSunday() {
    LocalDate localDate = LocalDate.of(2017, 07, 8);
    LocalDate nextSunday = localDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
     
    String expected = "2017-07-09";
     
    assertEquals(expected, nextSunday.toString());
}

@Test
public void whenAdjust_thenFourteenDaysAfterDate() {
    LocalDate localDate = LocalDate.of(2017, 07, 8);
    TemporalAdjuster temporalAdjuster = t -> t.plus(Period.ofDays(14));
    LocalDate result = localDate.with(temporalAdjuster);
     
    String fourteenDaysAfterDate = "2017-07-22";
     
    assertEquals(fourteenDaysAfterDate, result.toString());
}


static TemporalAdjuster NEXT_WORKING_DAY = TemporalAdjusters.ofDateAdjuster(date -> {
    DayOfWeek dayOfWeek = date.getDayOfWeek();
    int daysToAdd;
    if (dayOfWeek == DayOfWeek.FRIDAY)
        daysToAdd = 3;
    else if (dayOfWeek == DayOfWeek.SATURDAY)
        daysToAdd = 2;
    else
        daysToAdd = 1;
    return today.plusDays(daysToAdd);
});


@Test
public void whenAdjust_thenNextWorkingDay() {
    LocalDate localDate = LocalDate.of(2017, 07, 8);
    TemporalAdjuster temporalAdjuster = NEXT_WORKING_DAY;
    LocalDate result = localDate.with(temporalAdjuster);
 
    assertEquals("2017-07-10", date.toString());
}


public class CustomTemporalAdjuster implements TemporalAdjuster {
 
    @Override
    public Temporal adjustInto(Temporal temporal) {
        DayOfWeek dayOfWeek 
          = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
         
        int daysToAdd;
        if (dayOfWeek == DayOfWeek.FRIDAY)
            daysToAdd = 3;
        else if (dayOfWeek == DayOfWeek.SATURDAY)
            daysToAdd = 2;
        else
            daysToAdd = 1;
        return temporal.plus(daysToAdd, ChronoUnit.DAYS);
    }
}

@Test
public void whenAdjustAndImplementInterface_thenNextWorkingDay() {
    LocalDate localDate = LocalDate.of(2017, 07, 8);
    CustomTemporalAdjuster temporalAdjuster = new CustomTemporalAdjuster();
    LocalDate nextWorkingDay = localDate.with(temporalAdjuster);
     
    assertEquals("2017-07-10", nextWorkingDay.toString());
}
Instant.now().atOffset(ZoneOffset.ofHours(8))// 获取北京时间

ZonedDateTime.of(LocalDateTime.of(2014, 1, 20, 3, 30, 20), ZoneId.of("+08"));

Timestamp timestamp =  new Timestamp(System.currentTimeMillis())

timestamp.toLocalDateTime()

timestamp.toInstant()


ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");

ZoneId systemZoneId = ZoneId.systemDefault();


Set<String> zoneIds = ZoneId.getAvailableZoneIds();

ZoneId oldToNewZoneId = TimeZone.getDefault().toZoneId();

LocalDateTime localDateTime = LocalDateTime.now();

ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, shanghaiZoneId);


另一种表示时区的方式是使用ZoneOffset,它是以当前时间和世界标准时间(UTC)/格林威治时间(GMT)的偏差来计算:

ZoneOffset zoneOffset = ZoneOffset.of("+09:00");

LocalDateTime localDateTime = LocalDateTime.now();

OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, zoneOffset);

Set<String> allZoneIds = ZoneId.getAvailableZoneIds();获取所有的ZoneId
package com.company;

import java.io.PrintStream;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class Main
{
  public static final String DEVICE_MONITOR_DATA_CACHE_NAME = "secm_device_monitor_data";
  public static final String DEVICE_MONITOR_DATA_AVAILABLE_DAYS_CACHE_NAME = "secm_device_monitor_data_available_days";
  public static final String DB_OPERATION_FAILED = "DB_OPERATION_FAILED";
  public static final String OPERATION_FAILED = "OPERATION_FAILED";
  public static final Long TOTAL_QUERY_TIMEOUT = Long.valueOf(60000L);

  public static final Long DEVICE_QUERY_RETRY_WAIT = Long.valueOf(1000L);
  public static final String DATE_DISPLAY_FORMAT = "yyyy.M.d";

  public static long getOneUiDayStartMillis(String dayStr)
    throws ParseException
  {
    try
    {
      date = new SimpleDateFormat("yyyy.M.d").parse(dayStr);
    }
    catch (ParseException e)
    {
      Date date;
      System.out.println("Parse date:" + dayStr + "Format:" + "yyyy.M.d");
      throw e;
    }
    Date date;
    ZonedDateTime dateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    return dateTime.with(LocalTime.MIN).toInstant().toEpochMilli();
  }

  public static long getOneUiDayEndMillis(String dayStr)
    throws ParseException
  {
    return getOneUiDayStartMillis(dayStr) + TimeUnit.DAYS.toMillis(1L) - 1L;
  }

  public static void main(String[] args)
  {
    try {
      long startMillis = getOneUiDayStartMillis("2018.8.2");
      long curMillis = System.currentTimeMillis();
      long endMillis = getOneUiDayEndMillis("2018.8.2");

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
      sdf.setTimeZone(TimeZone.getTimeZone("EDT"));
      String dateStr = "2018-08-02T00:00:01Z";
      Date date = sdf.parse(dateStr);
      Timestamp sqlTs = new Timestamp(date.getTime());
      System.out.println("millis of " + dateStr + "in EDT: " + sqlTs.getTime());

      System.out.println(" start:" + startMillis + " end: " + endMillis + " cur " + curMillis);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }
}