Because of the short battery life of user equipments (UEs), and the requirements for better quality of service have been more demanding, energy efficiency (EE) has emerged to be important in device-to-device (D2D) communications. In this paper, we consider a scenario, in which D2D UEs in a half-duplex decode-and-forward cognitive D2D communication underlying a traditional cellular network harvest energy and communicate with each other by using the spectrum allocated by the base station (BS). In order to develop a practical design, we achieve the optimal time switching (TS) ratio for energy harvesting. Besides that, we derive closedform expressions for outage probability, sum-bit error rate, average EE and instantaneous rate by considering the scenario when installing the BS near UEs or far from the UEs. Two communication types are enabled by TS-based protocol. Our numerical and simulation results prove that the data rate of the D2D communication can be significantly enhanced.