如何从Instant.now()获取DayOfWeek

时间:2019-05-29 15:47:30

标签: java java-8 dayofweek java.time

假设以下代码...

Instant x = Instant.now();

我如何从x获得星期几?

3 个答案:

答案 0 :(得分:4)

您必须将其转换为ZonedDateTime

Instant.now().atZone(ZoneId.systemDefault()).getDayOfWeek()

答案 1 :(得分:0)

我已将积分授予了techtabu,但最终我使用了atOffset。这是我结束的地方...

x => new { x.A, x.B, x.C }

我很惊讶Java8日期时间库有多么困难。相似概念的变化如此之多...

  • 即时
  • LocalDate
  • LocalTime
  • LocalDateTime
  • OffsetDateTime
  • ZoneOffset
  • ZonedDateTime

修辞问题:

Zulu和UTC是相同还是不同?

与Instant.now()相关联的时区是什么-结果表明Zulu?

为什么我不能像LocalDateTime一样操作Instant对象-方法相似但又不同?

ZonedDateTime和OffsetDateTime有何不同-它们似乎在解决相同的概念。

答案 2 :(得分:-2)

   98 |               ) : null}
   99 |             </aside>
> 100 |           </div>
      |                 ^
  101 |       );
  102 |       }
  103 |     }


import React, { Component } from 'react';
    // [..]
    import {
      handleInput,
      connectToChatkit,
      connectToRoom,
    } from './methods';
    import Dialog from './components/Dialog';
      import RoomList from './components/RoomList';

    import 'skeleton-css/css/normalize.css';
    import 'skeleton-css/css/skeleton.css';
    import './App.css';

    class App extends Component {
      constructor() {
        super();
        this.state = {
          userId: '',
          showLogin: true,
          isLoading: false,
          currentUser: null,
          currentRoom: null,
          rooms: [],
          roomUsers: [],
          roomName: null,
          messages: [],
          newMessage: '',
        };

        this.handleInput = handleInput.bind(this);
        this.connectToChatkit = connectToChatkit.bind(this);
        this.connectToRoom = connectToRoom.bind(this);
      }
      render() {
        const {
          userId,
          showLogin,
          rooms,
          currentRoom,
          currentUser,
          messages,
          newMessage,
          roomUsers,
          roomName,
        } = this.state;



        return (
          <div className="App">
            <aside className="sidebar left-sidebar">
              {currentUser ? (
                <div className="user-profile">
                  <span className="username">{currentUser.name}</span>
                  <span className="user-id">{`@${currentUser.id}`}</span>
                </div>
              ) : null}

              {currentRoom ? (
             <RoomList
               rooms={rooms}
               currentRoom={currentRoom}
               connectToRoom={this.connectToRoom}
               currentUser={currentUser}
             />
           ) : null}

         </aside>
         <section className="chat-screen">
         // update `.chat-header`
          <header className="chat-header">
             {currentRoom ? <h3>{roomName}</h3> : null}
           </header>

            <section className="chat-screen">
            <header className="chat-header"></header>
            <ul className="chat-messages"></ul>
            <footer className="chat-footer">
              <form className="message-form">
                <input
                  type="text"
                  name="newMessage"
                  className="message-input"
                  placeholder="Type your message and hit ENTER to send"
                />
              </form>
            </footer>
            </section>

            <aside className="sidebar right-sidebar">
              {showLogin ? (
                <Dialog
                  userId={userId}
                  handleInput={this.handleInput}
                  connectToChatkit={this.connectToChatkit}
                />
              ) : null}
            </aside>
          </div>
      );
      }
    }

将给您相同的结果